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