KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > editors > preview > StateDialog


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.editors.preview;
20
21 import java.awt.*;
22 import java.awt.event.*;
23
24 import javax.swing.*;
25
26 import org.openharmonise.commons.xml.*;
27 import org.openharmonise.vfs.context.*;
28 import org.openharmonise.vfs.gui.*;
29 import org.w3c.dom.*;
30
31
32 /**
33  * Dialog for choosing a state to use when previewing pages.
34  *
35  * @author matt treanor
36  * @version $Revision: 1.1 $
37  */

38 public class StateDialog
39     extends JDialog
40     implements LayoutManager, ActionListener, ContextListener, MouseListener {
41
42     private JButton m_okButton = null;
43     private JButton m_cancelButton = null;
44     private JTextArea m_stateLabel = null;
45     private JList m_stateList = null;
46     private XMLPrettyPrint m_printer = null;
47     
48     private StateContainer m_stateContainer = null;
49     
50     public StateDialog() {
51         m_stateContainer = new StateContainer();
52     }
53     /**
54      *
55      */

56     public StateDialog(Frame frame, String JavaDoc title, String JavaDoc sVFilePath) {
57         super(frame, title, true);
58         ContextHandler.getInstance().addListener(
59             ContextType.CONTEXT_SHUTDOWN,
60             this);
61         m_stateContainer = new StateContainer(sVFilePath);
62         //check if vfile has a scenario
63
Document doc = m_stateContainer.getMemberState(sVFilePath);
64         if (doc != null) {
65             m_stateContainer.setState(doc);
66         } else {
67             setup();
68         }
69     }
70     private void setup() {
71         m_printer = new XMLPrettyPrint();
72         
73         this.getContentPane().setLayout(this);
74         this.setSize(200, 300);
75
76         m_stateList = new JList();
77         refreshJList();
78         getContentPane().add(m_stateList);
79         m_stateList.addMouseListener(this);
80
81         this.m_okButton = new JButton("OK");
82         this.m_okButton.setActionCommand("OK");
83         this.m_okButton.addActionListener(this);
84         m_okButton.setEnabled(false);
85         this.getContentPane().add(m_okButton);
86
87         this.m_cancelButton = new JButton("Cancel");
88         this.m_cancelButton.setActionCommand("CANCEL");
89         this.m_cancelButton.addActionListener(this);
90         this.getContentPane().add(m_cancelButton);
91
92         this.m_stateLabel = new JTextArea("Select a state");
93         m_stateLabel.setOpaque(false);
94         this.getContentPane().add(this.m_stateLabel);
95
96         int x =
97             this.getGraphicsConfiguration().getBounds().width / 2
98                 - this.getSize().width / 2;
99         int y =
100             this.getGraphicsConfiguration().getBounds().height / 2
101                 - this.getSize().height / 2;
102
103         this.setLocation(x, y);
104
105     }
106     /* (non-Javadoc)
107      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
108      */

109     public void removeLayoutComponent(Component arg0) {
110     }
111     /* (non-Javadoc)
112      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
113      */

114     public void layoutContainer(Container arg0) {
115         m_stateList.setSize(170, 200);
116         m_stateList.setLocation(10, 30);
117         
118         m_stateLabel.setSize(170,20);
119         m_stateLabel.setLocation(10,10);
120
121         this.m_okButton.setSize(70, 20);
122         this.m_okButton.setLocation(10, 240);
123
124         this.m_cancelButton.setSize(90, 20);
125         this.m_cancelButton.setLocation(90, 240);
126     }
127     /* (non-Javadoc)
128      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
129      */

130     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
131     }
132     /* (non-Javadoc)
133      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
134      */

135     public Dimension minimumLayoutSize(Container arg0) {
136         return null;
137     }
138     /* (non-Javadoc)
139      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
140      */

141     public Dimension preferredLayoutSize(Container arg0) {
142         return null;
143     }
144     /* (non-Javadoc)
145      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
146      */

147     public void actionPerformed(ActionEvent ae) {
148         if (ae.getActionCommand().equals("OK")) {
149             doOK();
150         } else if (ae.getActionCommand().equals("CANCEL")) {
151             doCancel();
152         }
153     }
154     private void doOK() {
155         String JavaDoc scenario = (String JavaDoc) m_stateList.getSelectedValue();
156         if (scenario != null) {
157             m_stateContainer.addMemberToScenario(scenario);
158             m_stateContainer.save();
159             this.setVisible(false);
160         }
161     }
162     private void doCancel() {
163         m_stateContainer.setState(null);
164         this.hide();
165     }
166     public void refreshJList() {
167         this.m_stateList.setListData(m_stateContainer.getStates().keySet().toArray());
168     }
169     /**
170      * @return
171      */

172     public Document getState() {
173         return m_stateContainer.getState();
174     }
175     public void contextMessage(ContextEvent ce) {
176         if (ce.CONTEXT_TYPE == ContextType.CONTEXT_SHUTDOWN) {
177             m_stateContainer.save();
178         }
179     }
180     /* (non-Javadoc)
181      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
182      */

183     public void mouseClicked(MouseEvent me) {
184         if (me.getClickCount() > 0 && me.getComponent() == m_stateList) {
185             m_okButton.setEnabled(true);
186             int index = this.m_stateList.locationToIndex(me.getPoint());
187             String JavaDoc name = (String JavaDoc) m_stateList.getSelectedValue();
188             Document stateDoc = m_stateContainer.getState(name);
189             if (me.getClickCount() == 2) {
190                 doOK();
191             }
192         }
193
194     }
195     /* (non-Javadoc)
196      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
197      */

198     public void mouseEntered(MouseEvent arg0) {
199     }
200     /* (non-Javadoc)
201      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
202      */

203     public void mouseExited(MouseEvent arg0) {
204     }
205     /* (non-Javadoc)
206      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
207      */

208     public void mousePressed(MouseEvent arg0) {
209     }
210     /* (non-Javadoc)
211      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
212      */

213     public void mouseReleased(MouseEvent arg0) {
214     }
215     public static void main(String JavaDoc[] args) {
216         Frame frame = new Frame();
217         frame.setIconImage(
218             ((ImageIcon) IconManager
219                 .getInstance()
220                 .getIcon("16-page-definition.gif"))
221                 .getImage());
222         StateDialog sd = new StateDialog(frame, "state selector", "/webdav/Content/bob");
223         //test whether resourse has a scenario before showing
224
Document doc = sd.getState();
225         if(doc == null){
226             sd.show();
227             doc = sd.getState();
228         }
229         if (doc != null) {
230             System.out.println(doc.getDocumentElement().toString());
231         }
232
233         System.exit(0);
234     }
235 }
236
Popular Tags