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