View Javadoc

1   /*
2    * TextFontAction.java
3    *
4    * Created on 12. juni 2005, 22:38
5    */
6   
7   package com.vikash.firsttool.UI;
8   import com.vikash.firsttool.Diagram.*;
9   import javax.swing.*;
10  import java.awt.event.*;
11  import java.util.List;
12  import java.util.*;
13  import java.awt.*;
14  
15  import org.jgraph.JGraph;
16  import org.jgraph.graph.Port;
17  import org.jgraph.graph.CellView;
18  import org.jgraph.graph.AttributeMap;
19  import org.jgraph.graph.DefaultGraphModel;
20  import org.jgraph.graph.GraphConstants;
21  
22  /***
23   *
24   * @author  studajb
25   */
26  public class TextFontAction extends JComboBox{
27      MainFrame frame; 
28      ToolGraph graph;
29      AttributeMap map;
30      Map attributemap;
31      /*** Creates a new instance of TextFontAction */
32      public TextFontAction(MainFrame frame){
33          this.frame=frame;
34          GraphicsEnvironment gEnv =GraphicsEnvironment.getLocalGraphicsEnvironment();
35  	Object[] items=gEnv.getAvailableFontFamilyNames();
36          
37          setEditable(true);
38          setMaximumSize(new Dimension(90, 30));
39          setPreferredSize(new Dimension(90, 30));
40          setToolTipText("Font Type");
41                  for (int i = 0; i < items.length; i++) {
42  			Object item = items[i];
43  			if (item != null)
44  				addItem(item);
45  		}
46       this.addActionListener(new ActionListener()
47       {
48           public void actionPerformed(ActionEvent e){
49               String font=(String)((JComboBox)e.getSource()).getSelectedItem();
50               changeFont(font);
51               
52           }
53       });
54          
55      }
56      
57      public void changeFont(String fonttype) {
58           
59          String fontname=fonttype;
60          ToolInternalFrame internalframe=(ToolInternalFrame)frame.getDesktopPane().getSelectedFrame();
61               if(internalframe==null)  return; 
62           graph=internalframe.getGraph();
63          Object[] cells = DefaultGraphModel.getDescendants(graph.getModel(), graph.getSelectionCells()).toArray();
64          
65  		//Filter ports out
66  		java.util.List list = new ArrayList();
67  		for (int i = 0; i < cells.length; i++){
68  			if (!(cells[i] instanceof Port))
69  			list.add(cells[i]);
70                  }
71  		cells = list.toArray();
72                  
73          if (cells.length!=0){
74          
75  		for (int i = 0; i < cells.length; i++) {
76  			CellView view = (CellView)graph.getGraphLayoutCache().getMapping(cells[i],false);
77                          
78  			if (view != null) {
79                                 AttributeMap attributes=new AttributeMap();
80                                  Font font = GraphConstants.getFont(view.getAttributes());
81                                  GraphConstants.setFont(attributes, new Font(fontname, font.getStyle(), font.getSize()));
82                                  graph.getGraphLayoutCache().editCell(cells[i],attributes);
83                                  graph.startEditingAtCell(cells[i]);
84                                  graph.stopEditing();
85                                  
86  			}
87                         	}
88          
89  		
90                  
91          }
92  		 
93      }    
94      
95      
96  }