001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.editors.preview;
020:
021: import java.awt.*;
022: import java.awt.event.*;
023:
024: import javax.swing.*;
025:
026: import org.openharmonise.commons.xml.*;
027: import org.openharmonise.vfs.context.*;
028: import org.openharmonise.vfs.gui.*;
029: import org.w3c.dom.*;
030:
031: /**
032: * Dialog for choosing a state to use when previewing pages.
033: *
034: * @author matt treanor
035: * @version $Revision: 1.1 $
036: */
037: public class StateDialog extends JDialog implements LayoutManager,
038: ActionListener, ContextListener, MouseListener {
039:
040: private JButton m_okButton = null;
041: private JButton m_cancelButton = null;
042: private JTextArea m_stateLabel = null;
043: private JList m_stateList = null;
044: private XMLPrettyPrint m_printer = null;
045:
046: private StateContainer m_stateContainer = null;
047:
048: public StateDialog() {
049: m_stateContainer = new StateContainer();
050: }
051:
052: /**
053: *
054: */
055: public StateDialog(Frame frame, String title, String sVFilePath) {
056: super (frame, title, true);
057: ContextHandler.getInstance().addListener(
058: ContextType.CONTEXT_SHUTDOWN, this );
059: m_stateContainer = new StateContainer(sVFilePath);
060: //check if vfile has a scenario
061: Document doc = m_stateContainer.getMemberState(sVFilePath);
062: if (doc != null) {
063: m_stateContainer.setState(doc);
064: } else {
065: setup();
066: }
067: }
068:
069: private void setup() {
070: m_printer = new XMLPrettyPrint();
071:
072: this .getContentPane().setLayout(this );
073: this .setSize(200, 300);
074:
075: m_stateList = new JList();
076: refreshJList();
077: getContentPane().add(m_stateList);
078: m_stateList.addMouseListener(this );
079:
080: this .m_okButton = new JButton("OK");
081: this .m_okButton.setActionCommand("OK");
082: this .m_okButton.addActionListener(this );
083: m_okButton.setEnabled(false);
084: this .getContentPane().add(m_okButton);
085:
086: this .m_cancelButton = new JButton("Cancel");
087: this .m_cancelButton.setActionCommand("CANCEL");
088: this .m_cancelButton.addActionListener(this );
089: this .getContentPane().add(m_cancelButton);
090:
091: this .m_stateLabel = new JTextArea("Select a state");
092: m_stateLabel.setOpaque(false);
093: this .getContentPane().add(this .m_stateLabel);
094:
095: int x = this .getGraphicsConfiguration().getBounds().width / 2
096: - this .getSize().width / 2;
097: int y = this .getGraphicsConfiguration().getBounds().height / 2
098: - this .getSize().height / 2;
099:
100: this .setLocation(x, y);
101:
102: }
103:
104: /* (non-Javadoc)
105: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
106: */
107: public void removeLayoutComponent(Component arg0) {
108: }
109:
110: /* (non-Javadoc)
111: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
112: */
113: public void layoutContainer(Container arg0) {
114: m_stateList.setSize(170, 200);
115: m_stateList.setLocation(10, 30);
116:
117: m_stateLabel.setSize(170, 20);
118: m_stateLabel.setLocation(10, 10);
119:
120: this .m_okButton.setSize(70, 20);
121: this .m_okButton.setLocation(10, 240);
122:
123: this .m_cancelButton.setSize(90, 20);
124: this .m_cancelButton.setLocation(90, 240);
125: }
126:
127: /* (non-Javadoc)
128: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
129: */
130: public void addLayoutComponent(String arg0, Component arg1) {
131: }
132:
133: /* (non-Javadoc)
134: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
135: */
136: public Dimension minimumLayoutSize(Container arg0) {
137: return null;
138: }
139:
140: /* (non-Javadoc)
141: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
142: */
143: public Dimension preferredLayoutSize(Container arg0) {
144: return null;
145: }
146:
147: /* (non-Javadoc)
148: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
149: */
150: public void actionPerformed(ActionEvent ae) {
151: if (ae.getActionCommand().equals("OK")) {
152: doOK();
153: } else if (ae.getActionCommand().equals("CANCEL")) {
154: doCancel();
155: }
156: }
157:
158: private void doOK() {
159: String scenario = (String) m_stateList.getSelectedValue();
160: if (scenario != null) {
161: m_stateContainer.addMemberToScenario(scenario);
162: m_stateContainer.save();
163: this .setVisible(false);
164: }
165: }
166:
167: private void doCancel() {
168: m_stateContainer.setState(null);
169: this .hide();
170: }
171:
172: public void refreshJList() {
173: this .m_stateList.setListData(m_stateContainer.getStates()
174: .keySet().toArray());
175: }
176:
177: /**
178: * @return
179: */
180: public Document getState() {
181: return m_stateContainer.getState();
182: }
183:
184: public void contextMessage(ContextEvent ce) {
185: if (ce.CONTEXT_TYPE == ContextType.CONTEXT_SHUTDOWN) {
186: m_stateContainer.save();
187: }
188: }
189:
190: /* (non-Javadoc)
191: * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
192: */
193: public void mouseClicked(MouseEvent me) {
194: if (me.getClickCount() > 0 && me.getComponent() == m_stateList) {
195: m_okButton.setEnabled(true);
196: int index = this .m_stateList.locationToIndex(me.getPoint());
197: String name = (String) m_stateList.getSelectedValue();
198: Document stateDoc = m_stateContainer.getState(name);
199: if (me.getClickCount() == 2) {
200: doOK();
201: }
202: }
203:
204: }
205:
206: /* (non-Javadoc)
207: * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
208: */
209: public void mouseEntered(MouseEvent arg0) {
210: }
211:
212: /* (non-Javadoc)
213: * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
214: */
215: public void mouseExited(MouseEvent arg0) {
216: }
217:
218: /* (non-Javadoc)
219: * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
220: */
221: public void mousePressed(MouseEvent arg0) {
222: }
223:
224: /* (non-Javadoc)
225: * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
226: */
227: public void mouseReleased(MouseEvent arg0) {
228: }
229:
230: public static void main(String[] args) {
231: Frame frame = new Frame();
232: frame.setIconImage(((ImageIcon) IconManager.getInstance()
233: .getIcon("16-page-definition.gif")).getImage());
234: StateDialog sd = new StateDialog(frame, "state selector",
235: "/webdav/Content/bob");
236: //test whether resourse has a scenario before showing
237: Document doc = sd.getState();
238: if (doc == null) {
239: sd.show();
240: doc = sd.getState();
241: }
242: if (doc != null) {
243: System.out.println(doc.getDocumentElement().toString());
244: }
245:
246: System.exit(0);
247: }
248: }
|