KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > dods > DodsInnerPanel


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common.dods;
24
25 // Kelp imports
26
import org.enhydra.kelp.common.event.WriteEvent;
27 import org.enhydra.kelp.common.event.WriteListener;
28 import org.enhydra.kelp.common.node.OtterNode;
29 import org.enhydra.kelp.common.node.OtterProject;
30 import org.enhydra.kelp.common.swing.AddinInnerPanel;
31
32 // ToolBox imports
33
import org.enhydra.tool.common.DataValidationException;
34 import org.enhydra.tool.common.ButtonPanel;
35 import org.enhydra.tool.common.ToolException;
36
37 // Standard imports
38
import java.awt.*;
39 import java.lang.ref.WeakReference JavaDoc;
40 import javax.swing.*;
41 import javax.swing.event.*;
42 import java.beans.*;
43
44 //
45
public class DodsInnerPanel extends AddinInnerPanel {
46
47     private int showIndex = 0;
48     private JTabbedPane tab = null;
49     private BorderLayout layoutMain = null;
50     private GeneralPanel generalPanel = null;
51     private WeakReference JavaDoc backRef = null;
52     private WeakReference JavaDoc nextRef = null;
53
54     public DodsInnerPanel() {
55         super();
56         try {
57             jbInit();
58             pmInit();
59         } catch (Exception JavaDoc ex) {
60             ex.printStackTrace();
61         }
62     }
63
64     // override AddinInnerPanel
65
public void read(OtterNode node) {
66         super.read(node);
67         DodsStep[] steps = new DodsStep[0];
68
69         steps = getSteps();
70         for (int i = 0; i < steps.length; i++) {
71             steps[i].read(getProject());
72         }
73
74     }
75
76     // override AddinInnerPanel
77
public void write(OtterNode node) throws DataValidationException {
78         super.write(node);
79
80         generalPanel.write(getProject());
81     }
82
83     // override AddinInnerPanel
84
public void initPreferredSize() {
85         Dimension d = new Dimension();
86
87         d.height = 500;
88         d.width = 600;
89         setPreferredSize(d);
90     }
91
92     // override AddinInnerPanel
93
public void clearAll() {
94         super.clearAll();
95         if (backRef != null) {
96             backRef.clear();
97         }
98         if (nextRef != null) {
99             nextRef.clear();
100         }
101         generalPanel.clearAll();
102         backRef = null;
103         nextRef = null;
104         generalPanel = null;
105     }
106
107     //
108
private void refresh() {
109         Component back = null;
110         Component next = null;
111         boolean backEnable = true;
112         boolean nextEnable = true;
113
114         int index = tab.getSelectedIndex();
115
116         if (index == 0) {
117             backEnable = false;
118         } else if (index == (tab.getTabCount() - 1)) {
119             nextEnable = false;
120         }
121         if (back != null) {
122             back.setEnabled(backEnable);
123         }
124         if (next != null) {
125             next.setEnabled(nextEnable);
126         }
127         generalPanel.refresh();
128     }
129
130     protected void back() {
131         int index = tab.getSelectedIndex();
132
133         if (index > 0) {
134             showIndex = --index;
135             tab.setSelectedIndex(showIndex);
136         }
137         refresh();
138     }
139
140     private WriteListener getWriteListener() {
141         return generalPanel.getWriteListener();
142     }
143
144     protected void generate(CoreDodsTool tool) {
145
146        if (getProject() != null) {
147
148             Generator generate = null;
149             try {
150                 WriteEvent clear;
151
152                 clear = new WriteEvent(this, WriteEvent.CLEAR, new String JavaDoc());
153                 trySave();
154                 generate = new Generator(getWriteListener());
155                 generate.setProject(getProject());
156                 generate.setEcho(true);
157                 generate.addProgressListener(tool.getProgressMeter());
158                 tool.getProgressMeter().addCancelListener(generate);
159                 getWriteListener().onWrite(clear);
160
161                 tool.openProgress();
162
163                 generate.setOwner(getWindow());
164                 generate.setDoneDialog(true);
165
166                 generate.build();
167
168             } catch (DataValidationException e) {
169                 JOptionPane.showMessageDialog(this, e.getValidationMessage());
170             } catch (ToolException e) {
171                 JOptionPane.showMessageDialog(this, e.getMessage());
172                 e.printStackTrace(System.err);
173             }
174         }
175     }
176
177     // override InnerPanel
178
protected Component[] getFirstFocusComponents() {
179         Component[] comps = new Component[0];
180         Component[] genComps = new Component[0];
181
182         genComps = generalPanel.getFirstFocusComponents();
183         comps = new Component[genComps.length + 1];
184         comps[0] = tab;
185         for (int i = 0; i < genComps.length; i++) {
186             comps[i + 1] = genComps[i];
187         }
188         return comps;
189     }
190
191     // override InnerPanel
192
public void save() {
193         if (getProject() == null) {
194             System.err.println("DodsInnerPanel.save() - project null");
195         } else {
196             try {
197                 trySave();
198             } catch (DataValidationException e) {
199                 System.err.println(e.getValidationMessage());
200             } catch (ToolException e) {
201                 System.err.println(e.getMessage());
202             }
203         }
204     }
205
206     private void trySave() throws DataValidationException, ToolException {
207         try {
208             write(getProject());
209         } catch (DataValidationException e) {
210         }
211     }
212
213     //
214
private void pmInit() {
215         generalPanel = new GeneralPanel();
216         addInstructor(generalPanel);
217     }
218
219     private void addInstructor(Instructor i) {
220         InstructPanel ip = null;
221
222         if (i instanceof JPanel) {
223             ip = new InstructPanel();
224             ip.addInstuctor(i);
225             tab.add(ip, i.getTab());
226         } else {
227             throw new RuntimeException JavaDoc("Instructor not a JPanel: "
228                                        + i.getTitle());
229         }
230     }
231
232     private void jbInit() throws Exception JavaDoc {
233         tab = (JTabbedPane) Beans.instantiate(getClass().getClassLoader(),
234                                               JTabbedPane.class.getName());
235         layoutMain =
236             (BorderLayout) Beans.instantiate(getClass().getClassLoader(),
237                                              BorderLayout.class.getName());
238         this.setLayout(layoutMain);
239         this.add(tab, BorderLayout.CENTER);
240     }
241
242     private class TabChangeListener implements ChangeListener {
243         public void stateChanged(ChangeEvent e) {
244             if (!tab.getSelectedComponent().isShowing()) {
245                 int selectIndex = tab.getSelectedIndex();
246
247                 if (selectIndex > showIndex) {
248                     tab.setSelectedIndex(showIndex);
249                     for (int i = showIndex; i < selectIndex; i++) {
250                             break;
251                     }
252                     showIndex = tab.getSelectedIndex();
253                 } else if (selectIndex < showIndex) {
254                     showIndex = selectIndex;
255                 }
256             }
257             refresh();
258         }
259
260     }
261     private DodsStep[] getSteps() {
262         DodsStep[] steps = new DodsStep[1];
263
264         steps[0] = generalPanel;
265         return steps;
266     }
267
268 }
269
Popular Tags