View Javadoc

1   /*
2    * TextFontStyleAction.java
3    *
4    * Created on 27. juni 2005, 16:41
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 TextFontStyleAction extends JComboBox{
27      MainFrame frame;
28      ToolGraph graph;
29      AttributeMap map;
30      Map attributemap;
31      /*** Creates a new instance of TextFontAction */
32      public TextFontStyleAction(MainFrame frame){
33          this.frame=frame;
34          setEditable(true);
35          setMaximumSize(new Dimension(80, 30));
36          setPreferredSize(new Dimension(80, 30));
37          setToolTipText("Font Style");
38           Object[] items=new Object[]{"Normal","Bold","Italic"}; 
39                  for (int i = 0; i < items.length; i++) {
40  			Object item = items[i];
41  			if (item != null)
42  				addItem(item);
43  		}
44       this.addActionListener(new ActionListener()
45       {
46           public void actionPerformed(ActionEvent e){
47               String font=(String)((JComboBox)e.getSource()).getSelectedItem();
48               changeFontStyle(font);
49               
50           }
51       });
52          
53      }
54      
55      public void changeFontStyle(String fonttype) {
56         int style;  
57         if(fonttype=="Normal")style=Font.PLAIN;
58         else if(fonttype=="Bold")style=Font.BOLD;
59         else style=Font.ITALIC;
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  		//Filter ports out
65  		java.util.List list = new ArrayList();
66  		for (int i = 0; i < cells.length; i++)
67  			if (!(cells[i] instanceof Port))
68  			list.add(cells[i]);
69  		cells = list.toArray();
70          if (cells.length!=0){
71          
72  		for (int i = 0; i < cells.length; i++) {
73  			CellView view = (CellView)graph.getGraphLayoutCache().getMapping(cells[i],false);
74                          
75  			if (view != null) {
76                                 AttributeMap attributes=new AttributeMap();
77                                  Font font = GraphConstants.getFont(view.getAllAttributes());
78                                  GraphConstants.setFont(attributes, font.deriveFont(style));
79  				graph.getGraphLayoutCache().editCell(cells[i],attributes);
80                                  graph.startEditingAtCell(cells[i]);
81                                  graph.stopEditing();
82  			}
83                         	}
84          
85  		
86          
87          }
88  		 
89      }    
90      
91      
92  }
93