1 | /* |
---|
2 | * $Id: PlotMarkDialog.java,v 1.7 2003/09/17 20:32:10 dwd Exp $ |
---|
3 | * |
---|
4 | * This software is provided by NOAA for full, free and open release. It is |
---|
5 | * understood by the recipient/user that NOAA assumes no liability for any |
---|
6 | * errors contained in the code. Although this software is released without |
---|
7 | * conditions or restrictions in its use, it is expected that appropriate |
---|
8 | * credit be given to its author and to the National Oceanic and Atmospheric |
---|
9 | * Administration should the software be included by the recipient as an |
---|
10 | * element in other product development. |
---|
11 | */ |
---|
12 | package gov.noaa.pmel.sgt.swing.prop; |
---|
13 | |
---|
14 | import javax.swing.*; |
---|
15 | import java.awt.*; |
---|
16 | |
---|
17 | import gov.noaa.pmel.sgt.swing.PlotMarkIcon; |
---|
18 | |
---|
19 | /** |
---|
20 | * Provides a dialog to graphically select a <code>PlotMark</code> |
---|
21 | * code. |
---|
22 | * |
---|
23 | * @author Donald Denbo |
---|
24 | * @version $Revision: 1.7 $, $Date: 2003/09/17 20:32:10 $ |
---|
25 | * @since 2.0 |
---|
26 | */ |
---|
27 | public class PlotMarkDialog extends JDialog { |
---|
28 | private static final int numMarks = 51; |
---|
29 | private int result_; |
---|
30 | private int mark_; |
---|
31 | private JToggleButton[] buttons_ = new JToggleButton[numMarks]; |
---|
32 | /** OK button selected */ |
---|
33 | public static int OK_RESPONSE = 1; |
---|
34 | /** Cancel button selected */ |
---|
35 | public static int CANCEL_RESPONSE = 2; |
---|
36 | /** |
---|
37 | * Constructor |
---|
38 | */ |
---|
39 | public PlotMarkDialog(Frame parent) { |
---|
40 | super(parent); |
---|
41 | try { |
---|
42 | jbInit(); |
---|
43 | } catch(Exception ex) { |
---|
44 | ex.printStackTrace(); |
---|
45 | } |
---|
46 | } |
---|
47 | void jbInit() throws Exception { |
---|
48 | getContentPane().setLayout(new BorderLayout(0,0)); |
---|
49 | setSize(288,201); |
---|
50 | setVisible(false); |
---|
51 | buttonPanel.setBorder(etchedBorder1); |
---|
52 | buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER,5,5)); |
---|
53 | getContentPane().add(buttonPanel, "South"); |
---|
54 | buttonPanel.setBounds(0,162,288,39); |
---|
55 | okButton.setText("OK"); |
---|
56 | okButton.setActionCommand("OK"); |
---|
57 | buttonPanel.add(okButton); |
---|
58 | okButton.setBounds(79,7,51,25); |
---|
59 | cancelButton.setText("Cancel"); |
---|
60 | cancelButton.setActionCommand("Cancel"); |
---|
61 | buttonPanel.add(cancelButton); |
---|
62 | cancelButton.setBounds(135,7,73,25); |
---|
63 | //$$ etchedBorder1.move(0,300); |
---|
64 | mainPanel.setLayout(new GridLayout(4,11,0,0)); |
---|
65 | getContentPane().add(mainPanel, "Center"); |
---|
66 | mainPanel.setBounds(0,0,288,162); |
---|
67 | setTitle("Select a Mark"); |
---|
68 | |
---|
69 | SymWindow aSymWindow = new SymWindow(); |
---|
70 | this.addWindowListener(aSymWindow); |
---|
71 | SymAction lSymAction = new SymAction(); |
---|
72 | cancelButton.addActionListener(lSymAction); |
---|
73 | okButton.addActionListener(lSymAction); |
---|
74 | } |
---|
75 | /** Used internally */ |
---|
76 | public void addNotify() { |
---|
77 | // Record the size of the window prior to calling parents addNotify. |
---|
78 | Dimension d = getSize(); |
---|
79 | |
---|
80 | super.addNotify(); |
---|
81 | |
---|
82 | if (fComponentsAdjusted) |
---|
83 | return; |
---|
84 | |
---|
85 | // Adjust components according to the insets |
---|
86 | Insets ins = getInsets(); |
---|
87 | setSize(ins.left + ins.right + d.width, ins.top + ins.bottom + d.height); |
---|
88 | Component components[] = getContentPane().getComponents(); |
---|
89 | for (int i = 0; i < components.length; i++) { |
---|
90 | Point p = components[i].getLocation(); |
---|
91 | p.translate(ins.left, ins.top); |
---|
92 | components[i].setLocation(p); |
---|
93 | } |
---|
94 | fComponentsAdjusted = true; |
---|
95 | } |
---|
96 | |
---|
97 | // Used for addNotify check. |
---|
98 | boolean fComponentsAdjusted = false; |
---|
99 | /** |
---|
100 | * Constructor |
---|
101 | */ |
---|
102 | public PlotMarkDialog(String title) { |
---|
103 | this(); |
---|
104 | setTitle(title); |
---|
105 | } |
---|
106 | /** |
---|
107 | * Default constructor |
---|
108 | */ |
---|
109 | public PlotMarkDialog() { |
---|
110 | this((Frame)null); |
---|
111 | } |
---|
112 | /** |
---|
113 | * Make the dialog visible. |
---|
114 | */ |
---|
115 | public void setVisible(boolean b) { |
---|
116 | if(b) { |
---|
117 | setLocation(50, 50); |
---|
118 | } |
---|
119 | super.setVisible(b); |
---|
120 | } |
---|
121 | |
---|
122 | class SymWindow extends java.awt.event.WindowAdapter { |
---|
123 | public void windowClosing(java.awt.event.WindowEvent event) { |
---|
124 | Object object = event.getSource(); |
---|
125 | if (object == PlotMarkDialog.this) |
---|
126 | PlotMarkDialog_WindowClosing(event); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | void PlotMarkDialog_WindowClosing(java.awt.event.WindowEvent event) { |
---|
131 | dispose(); |
---|
132 | } |
---|
133 | |
---|
134 | javax.swing.JPanel buttonPanel = new javax.swing.JPanel(); |
---|
135 | javax.swing.JButton okButton = new javax.swing.JButton(); |
---|
136 | javax.swing.JButton cancelButton = new javax.swing.JButton(); |
---|
137 | javax.swing.border.EtchedBorder etchedBorder1 = new javax.swing.border.EtchedBorder(); |
---|
138 | javax.swing.JPanel mainPanel = new javax.swing.JPanel(); |
---|
139 | |
---|
140 | class SymAction implements java.awt.event.ActionListener { |
---|
141 | public void actionPerformed(java.awt.event.ActionEvent event) { |
---|
142 | Object object = event.getSource(); |
---|
143 | if (object == cancelButton) |
---|
144 | cancelButton_actionPerformed(event); |
---|
145 | else if (object == okButton) |
---|
146 | okButton_actionPerformed(event); |
---|
147 | |
---|
148 | |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | void cancelButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
153 | this.setVisible(false); |
---|
154 | result_ = CANCEL_RESPONSE; |
---|
155 | } |
---|
156 | |
---|
157 | void okButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
158 | result_ = OK_RESPONSE; |
---|
159 | for(int i=0; i < numMarks; i++) { |
---|
160 | if(buttons_[i].isSelected()) { |
---|
161 | mark_ = i+1; |
---|
162 | } |
---|
163 | } |
---|
164 | this.setVisible(false); |
---|
165 | } |
---|
166 | /** |
---|
167 | * Test entry point |
---|
168 | */ |
---|
169 | public static void main(String[] args) { |
---|
170 | PlotMarkDialog la = new PlotMarkDialog(); |
---|
171 | la.setTitle("Test PlotMark Dialog"); |
---|
172 | la.setVisible(true); |
---|
173 | } |
---|
174 | /** |
---|
175 | * Show the dialog and wait for a response |
---|
176 | * |
---|
177 | * @param mark initial <code>PlotMark</code> code |
---|
178 | * @return result, either CANCEL_RESPONSE or OK_RESPONSE |
---|
179 | */ |
---|
180 | public int showDialog(int mark) { |
---|
181 | mark_ = mark; |
---|
182 | createButtons(); |
---|
183 | result_ = CANCEL_RESPONSE; |
---|
184 | setModal(true); |
---|
185 | super.setVisible(true); |
---|
186 | return result_; |
---|
187 | } |
---|
188 | /** |
---|
189 | * Set initial mark. |
---|
190 | */ |
---|
191 | public void setMark(int mark) { |
---|
192 | mark_ = mark; |
---|
193 | } |
---|
194 | /** |
---|
195 | * Get the selected <code>PlotMark</code> code. |
---|
196 | */ |
---|
197 | public int getMark() { |
---|
198 | return mark_; |
---|
199 | } |
---|
200 | |
---|
201 | private void createButtons() { |
---|
202 | PlotMarkIcon pmi; |
---|
203 | ButtonGroup group = new ButtonGroup(); |
---|
204 | for(int i=0; i < numMarks; i++) { |
---|
205 | pmi = new PlotMarkIcon(i+1); |
---|
206 | buttons_[i] = new JToggleButton(pmi); |
---|
207 | buttons_[i].setName(Integer.toString(i+1)); |
---|
208 | group.add(buttons_[i]); |
---|
209 | mainPanel.add(buttons_[i]); |
---|
210 | } |
---|
211 | buttons_[mark_-1].setSelected(true); |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | } |
---|