]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/style/AWTStyleDialog.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / style / AWTStyleDialog.java
1 package org.simantics.modeling.ui.actions.style;\r
2 \r
3 import java.awt.BorderLayout;\r
4 import java.awt.Color;\r
5 import java.awt.FlowLayout;\r
6 import java.awt.Font;\r
7 import java.awt.Frame;\r
8 import java.awt.event.ActionEvent;\r
9 import java.awt.event.ActionListener;\r
10 import java.awt.event.WindowEvent;\r
11 import java.awt.event.WindowListener;\r
12 \r
13 import javax.swing.JButton;\r
14 import javax.swing.JColorChooser;\r
15 import javax.swing.JDialog;\r
16 import javax.swing.JPanel;\r
17 import javax.swing.JTabbedPane;\r
18 \r
19 import org.simantics.utils.strings.format.MetricsFormat;\r
20 \r
21 public class AWTStyleDialog extends JDialog {\r
22 \r
23         private static final long serialVersionUID = 6836378345175793069L;\r
24         \r
25         private FontChooser fontChooser;\r
26         private JColorChooser colorChooser;\r
27         private MetricsEditor metricsEditor;\r
28         \r
29         private boolean cancelled = true;\r
30         \r
31         private boolean useFont = true;\r
32         private boolean useColor = true;\r
33         private boolean useFormat = true;\r
34         \r
35         public AWTStyleDialog(Frame owner,boolean useFont, boolean useColor, boolean useFormat) {\r
36                 super(owner,"Style",true);\r
37                 this.useFont = useFont;\r
38                 this.useColor = useColor;\r
39                 this.useFormat = useFormat;\r
40                 createContents();\r
41         }\r
42         \r
43         public AWTStyleDialog(boolean useFont, boolean useColor, boolean useFormat) {\r
44                 super();\r
45                 setTitle("Style");\r
46                 setModal(true);\r
47                 this.useFont = useFont;\r
48                 this.useColor = useColor;\r
49                 this.useFormat = useFormat;\r
50                 createContents();\r
51         }\r
52         \r
53         public void setStartFont(Font font) {\r
54                 if (!useFont)\r
55                         throw new RuntimeException("Dialog is not configured with font support");\r
56                 fontChooser.setCurrentFont(font);\r
57         }\r
58         \r
59         public void setStartColor(Color color) {\r
60                 if (!useColor)\r
61                         throw new RuntimeException("Dialog is not configured with color support");\r
62                 colorChooser.setColor(color);\r
63         }\r
64         \r
65         public void setStartFormat(MetricsFormat format) {\r
66                 if (!useFormat)\r
67                         throw new RuntimeException("Dialog is not configured with format support");\r
68                 metricsEditor.setMetricsFormat(format);\r
69         }\r
70         \r
71         private void createContents() {\r
72                 \r
73                 JTabbedPane tabbedPane = new JTabbedPane();\r
74                 getContentPane().add(tabbedPane,BorderLayout.CENTER);\r
75                 if (useFont)\r
76                         tabbedPane.addTab("Font", fontChooser = new FontChooser("Sample text"));\r
77                 if (useColor)\r
78                         tabbedPane.addTab("Color",colorChooser = new JColorChooser(new Color(0, 0, 0)));\r
79                 if (useFormat)\r
80                         tabbedPane.addTab("Metrics",metricsEditor = new MetricsEditor());\r
81                 \r
82                 JPanel controlPanel = new JPanel();\r
83                 getContentPane().add(controlPanel,BorderLayout.SOUTH);\r
84                 controlPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));\r
85                 \r
86                 JButton okButton = new JButton("OK");\r
87                 controlPanel.add(okButton);\r
88                 okButton.addActionListener(new ActionListener() {\r
89                         \r
90                         @Override\r
91                         public void actionPerformed(ActionEvent e) {\r
92                                 cancelled = false;\r
93                                 AWTStyleDialog.this.setVisible(false);\r
94                                 AWTStyleDialog.this.dispose();\r
95                         }\r
96                 });\r
97                 \r
98                 JButton cancelButton = new JButton("Cancel");\r
99                 controlPanel.add(cancelButton);\r
100                 cancelButton.addActionListener(new ActionListener() {\r
101                         \r
102                         @Override\r
103                         public void actionPerformed(ActionEvent e) {\r
104                                 AWTStyleDialog.this.setVisible(false);\r
105                                 AWTStyleDialog.this.dispose();\r
106                         }\r
107                 });\r
108                 \r
109                 \r
110                 this.addWindowListener(new WindowListener() {\r
111                         \r
112                         @Override\r
113                         public void windowOpened(WindowEvent arg0) {}\r
114                         \r
115                         @Override\r
116                         public void windowIconified(WindowEvent arg0) {}\r
117                         \r
118                         @Override\r
119                         public void windowDeiconified(WindowEvent arg0) {}\r
120                         \r
121                         @Override\r
122                         public void windowDeactivated(WindowEvent arg0) {}\r
123                         \r
124                         @Override\r
125                         public void windowClosing(WindowEvent arg0) {\r
126                                 if (metricsEditor != null)\r
127                                         metricsEditor.dispose();\r
128                         }\r
129                         \r
130                         @Override\r
131                         public void windowClosed(WindowEvent arg0) {}\r
132                         \r
133                         @Override\r
134                         public void windowActivated(WindowEvent arg0) {}\r
135                 });\r
136         }\r
137         \r
138         public boolean isCancelled() {\r
139                 return cancelled;\r
140         }\r
141         \r
142         public Font getFont() {\r
143                 if (fontChooser != null)\r
144                         return fontChooser.getFont();\r
145                 return null;\r
146         }\r
147         \r
148         public Color getColor() {\r
149                 if (colorChooser != null)\r
150                         return colorChooser.getColor();\r
151                 return null;\r
152         }\r
153         \r
154         public MetricsFormat getFormat() {\r
155                 return metricsEditor != null ? metricsEditor.getFormat() : null;\r
156         }\r
157 }\r
158 \r