KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > displaycomponents > table > order > OrderDialog


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
20 package org.openharmonise.him.displaycomponents.table.order;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.net.*;
25 import java.util.*;
26
27 import javax.swing.*;
28
29 import org.openharmonise.him.actions.dir.*;
30 import org.openharmonise.him.context.StateHandler;
31 import org.openharmonise.him.window.messages.builders.*;
32 import org.openharmonise.him.window.swing.ResourcePathCellRenderer;
33 import org.openharmonise.localversioningfilesystem.*;
34 import org.openharmonise.vfs.*;
35 import org.openharmonise.vfs.context.*;
36 import org.openharmonise.vfs.gui.*;
37 import org.openharmonise.vfs.status.*;
38
39
40 /**
41  * Dialog for setting the server side order of resources in a collection.
42  *
43  * @author Matthew Large
44  * @version $Revision: 1.1 $
45  *
46  */

47 public class OrderDialog extends JDialog implements LayoutManager, ActionListener, ContextListener {
48
49     /**
50      * OK button.
51      */

52     private JButton m_okButton = null;
53     
54     /**
55      * Cancel button.
56      */

57     private JButton m_cancelButton = null;
58     
59     /**
60      * Move resource up button.
61      */

62     private JButton m_upButton = null;
63     
64     /**
65      * Move resource down button.
66      */

67     private JButton m_downButton = null;
68     
69     /**
70      * List of resources.
71      */

72     private JList m_fileList = null;
73     
74     /**
75      * Collection to be order.
76      */

77     private VirtualFile m_vfDir = null;
78     
79     /**
80      * Scroller panel.
81      */

82     private JPanel m_innerPane = null;
83     
84     /**
85      * true if the order was changed.
86      */

87     private boolean m_bOrderChanaged = false;
88
89     /**
90      * Constructs a new order dialog.
91      *
92      * @param arg0 Owning frame
93      * @param arg1 Dialog title
94      * @param vfDir Collection to be ordered
95      * @throws HeadlessException
96      */

97     public OrderDialog(Frame arg0, String JavaDoc arg1, VirtualFile vfDir) throws HeadlessException {
98         super(arg0, arg1, true);
99         this.m_vfDir = vfDir;
100         this.setup();
101     }
102     
103     public static void main(String JavaDoc[] args) {
104         JFrame frame = new JFrame();
105         frame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
106
107         ToolTipManager.sharedInstance().setInitialDelay(1000);
108         ToolTipManager.sharedInstance().setDismissDelay(500);
109         
110         AbstractVirtualFileSystem vfs = null;
111         try {
112             vfs =
113                 new LocalVersioningFileSystem(
114                     new URI("file:/C:/ContentManagerTesting2/webdav/"));
115         } catch (URISyntaxException e) {
116             e.printStackTrace();
117         }
118         VirtualFile vfDir = vfs.getVirtualFile("/webdav/Assets/Web Resources/linkdir1").getResource();
119         
120         OrderDialog dialog = new OrderDialog(frame, "Set Default order", vfDir);
121         dialog.show();
122     }
123
124     /**
125      * Configures this dialog.
126      */

127     private void setup() {
128         ContextHandler.getInstance().addListener(ContextType.CONTEXT_APP_FOCUS, this);
129         
130         this.setResizable(false);
131         
132         this.getContentPane().setLayout(this);
133         
134         this.setSize(250,275);
135         int x = this.getGraphicsConfiguration().getBounds().width/2-this.getSize().width/2;
136         int y = this.getGraphicsConfiguration().getBounds().height/2-this.getSize().height/2;
137         
138         this.setLocation(x, y);
139
140         String JavaDoc fontName = "Dialog";
141         int fontSize = 11;
142         Font font = new Font(fontName, Font.PLAIN, fontSize);
143         this.getContentPane().setBackground(new Color(224,224,224));
144
145         m_fileList = new JList();
146         m_fileList.setCellRenderer( new ResourcePathCellRenderer(this.m_vfDir.getVFS()) );
147         m_fileList.setFont(font);
148         
149         ArrayList children = new ArrayList();
150         Iterator itor = this.m_vfDir.getChildren().iterator();
151         while (itor.hasNext()) {
152             String JavaDoc element = (String JavaDoc) itor.next();
153             VirtualFile vfChild = this.m_vfDir.getVFS().getVirtualFile(element).getResource();
154             children.add(vfChild.getFullPath());
155         }
156         
157         m_fileList.setListData(children.toArray());
158         m_fileList.setBorder(BorderFactory.createEtchedBorder());
159         
160         m_innerPane = new JPanel();
161         m_innerPane.setLayout(new BorderLayout());
162         JScrollPane scroller = new JScrollPane(m_fileList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
163         scroller.setBorder(BorderFactory.createEmptyBorder());
164         m_innerPane.add(scroller);
165         this.getContentPane().add(m_innerPane);
166         
167         m_upButton = new JButton();
168         m_upButton.setToolTipText("Move selected resource up list");
169         m_upButton.setIcon( IconManager.getInstance().getIcon("16-command-up.png") );
170         m_upButton.setActionCommand("UP");
171         m_upButton.addActionListener(this);
172         this.getContentPane().add(m_upButton);
173         
174         m_downButton = new JButton();
175         m_downButton.setToolTipText("Move selected resource down list");
176         m_downButton.setIcon( IconManager.getInstance().getIcon("16-command-down.png") );
177         m_downButton.setActionCommand("DOWN");
178         m_downButton.addActionListener(this);
179         this.getContentPane().add(m_downButton);
180         
181         m_okButton = new JButton("Ok");
182         m_okButton.setActionCommand("OK");
183         m_okButton.setFont(font);
184         m_okButton.addActionListener(this);
185         this.getContentPane().add(m_okButton);
186         
187         m_cancelButton = new JButton("Cancel");
188         m_cancelButton.setActionCommand("CANCEL");
189         m_cancelButton.setFont(font);
190         m_cancelButton.addActionListener(this);
191         this.getContentPane().add(m_cancelButton);
192     }
193     
194     /**
195      * Checks if the order has been changed.
196      *
197      * @return true if the order has been changed
198      */

199     public boolean isOrderChanged() {
200         return this.m_bOrderChanaged;
201     }
202     
203     /**
204      * @throws java.awt.HeadlessException
205      */

206     private OrderDialog() throws HeadlessException {
207         super();
208     }
209
210     /**
211      * @param arg0
212      * @throws java.awt.HeadlessException
213      */

214     private OrderDialog(Dialog arg0) throws HeadlessException {
215         super(arg0);
216     }
217
218     /**
219      * @param arg0
220      * @param arg1
221      * @throws java.awt.HeadlessException
222      */

223     private OrderDialog(Dialog arg0, boolean arg1) throws HeadlessException {
224         super(arg0, arg1);
225     }
226
227     /**
228      * @param arg0
229      * @throws java.awt.HeadlessException
230      */

231     private OrderDialog(Frame arg0) throws HeadlessException {
232         super(arg0);
233     }
234
235     /**
236      * @param arg0
237      * @param arg1
238      * @throws java.awt.HeadlessException
239      */

240     private OrderDialog(Frame arg0, boolean arg1) throws HeadlessException {
241         super(arg0, arg1);
242     }
243
244     /**
245      * @param arg0
246      * @param arg1
247      * @throws java.awt.HeadlessException
248      */

249     private OrderDialog(Dialog arg0, String JavaDoc arg1) throws HeadlessException {
250         super(arg0, arg1);
251     }
252
253     /**
254      * @param arg0
255      * @param arg1
256      * @param arg2
257      * @throws java.awt.HeadlessException
258      */

259     private OrderDialog(Dialog arg0, String JavaDoc arg1, boolean arg2)
260         throws HeadlessException {
261         super(arg0, arg1, arg2);
262     }
263
264     /**
265      * @param arg0
266      * @param arg1
267      * @param arg2
268      * @throws java.awt.HeadlessException
269      */

270     private OrderDialog(Frame arg0, String JavaDoc arg1, boolean arg2)
271         throws HeadlessException {
272         super(arg0, arg1, arg2);
273     }
274
275     /**
276      * @param arg0
277      * @param arg1
278      * @param arg2
279      * @param arg3
280      * @throws java.awt.HeadlessException
281      */

282     private OrderDialog(
283         Dialog arg0,
284         String JavaDoc arg1,
285         boolean arg2,
286         GraphicsConfiguration arg3)
287         throws HeadlessException {
288         super(arg0, arg1, arg2, arg3);
289     }
290
291     /**
292      * @param arg0
293      * @param arg1
294      * @param arg2
295      * @param arg3
296      */

297     private OrderDialog(
298         Frame arg0,
299         String JavaDoc arg1,
300         boolean arg2,
301         GraphicsConfiguration arg3) {
302         super(arg0, arg1, arg2, arg3);
303     }
304
305     /* (non-Javadoc)
306      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
307      */

308     public void removeLayoutComponent(Component arg0) {
309         
310     }
311
312     /* (non-Javadoc)
313      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
314      */

315     public void layoutContainer(Container arg0) {
316         
317         m_innerPane.setLocation(10,10);
318         m_innerPane.setSize(200,200);
319         
320         m_upButton.setLocation(215, 80);
321         m_upButton.setSize( 25,25 );
322         
323         m_downButton.setLocation(215, 110);
324         m_downButton.setSize( 25,25 );
325
326         m_okButton.setLocation(20, 220);
327         m_okButton.setSize(70,20);
328         
329         m_cancelButton.setLocation(105, 220);
330         m_cancelButton.setSize(70,20);
331     }
332
333     /* (non-Javadoc)
334      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
335      */

336     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
337         
338     }
339
340     /* (non-Javadoc)
341      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
342      */

343     public Dimension minimumLayoutSize(Container arg0) {
344         return this.getSize();
345     }
346
347     /* (non-Javadoc)
348      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
349      */

350     public Dimension preferredLayoutSize(Container arg0) {
351         return this.getSize();
352     }
353
354     /* (non-Javadoc)
355      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
356      */

357     public void actionPerformed(ActionEvent ae) {
358         if(ae.getActionCommand().equals("OK")) {
359             StateHandler.getInstance().addWait(this, "ORDER-COL");
360             StatusData status = new VFSStatus();
361             String JavaDoc sDirName =null;
362             status.setMethodName(ActionOrder.ACTION_NAME);
363             try {
364                 ListModel model = this.m_fileList.getModel();
365                 ArrayList values = new ArrayList();
366                 for (int i = 0; i < model.getSize(); i++) {
367                     values.add(model.getElementAt(i));
368                 }
369             
370                 StatusData statusData = this.m_vfDir.setChildrenOrder(values);
371                 status.addStatusData(statusData);
372                 
373                 this.hide();
374                 
375                 sDirName = this.m_vfDir.getVFS().getVirtualFileSystemView().getDisplayName(this.m_vfDir);
376
377             } catch (Exception JavaDoc e) {
378                 e.printStackTrace(System.err);
379                 status.setStatusLevel(StatusData.LEVEL_ERROR);
380                 sDirName = this.m_vfDir.getVFS().getVirtualFileSystemView().getDisplayName(this.m_vfDir);
381             } finally {
382                 VFSMessageBuilder.getInstance().fireMessage(ActionOrder.ACTION_NAME, status, sDirName);
383                 StateHandler.getInstance().removeWait(this, "ORDER-COL");
384             }
385         } else if(ae.getActionCommand().equals("CANCEL")) {
386             this.m_bOrderChanaged=false;
387             this.hide();
388         } else if(ae.getActionCommand().equals("UP")) {
389             ListModel model = this.m_fileList.getModel();
390             ArrayList values = new ArrayList();
391             for (int i = 0; i < model.getSize(); i++) {
392                 values.add(model.getElementAt(i));
393             }
394             int nIndex = this.m_fileList.getSelectedIndex();
395             if(nIndex>0) {
396                 String JavaDoc sValueUp = (String JavaDoc) values.get(nIndex);
397                 String JavaDoc sValueDown = (String JavaDoc) values.get(nIndex-1);
398                 values.set(nIndex-1, sValueUp);
399                 values.set(nIndex, sValueDown);
400                 this.m_fileList.setListData(values.toArray());
401                 this.m_fileList.setSelectedIndex(nIndex-1);
402             }
403             this.m_bOrderChanaged=true;
404             this.validateTree();
405             this.repaint();
406         } else if(ae.getActionCommand().equals("DOWN")) {
407             ListModel model = this.m_fileList.getModel();
408             ArrayList values = new ArrayList();
409             for (int i = 0; i < model.getSize(); i++) {
410                 values.add(model.getElementAt(i));
411             }
412             int nIndex = this.m_fileList.getSelectedIndex();
413             if(nIndex<model.getSize()-1) {
414                 String JavaDoc sValueDown = (String JavaDoc) values.get(nIndex);
415                 String JavaDoc sValueUp = (String JavaDoc) values.get(nIndex+1);
416                 values.set(nIndex+1, sValueDown);
417                 values.set(nIndex, sValueUp);
418                 this.m_fileList.setListData(values.toArray());
419                 this.m_fileList.setSelectedIndex(nIndex+1);
420             }
421             this.m_bOrderChanaged=true;
422             this.validateTree();
423             this.repaint();
424         }
425     }
426
427     /* (non-Javadoc)
428      * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
429      */

430     public void contextMessage(ContextEvent ce) {
431         if(ce.CONTEXT_TYPE==ContextType.CONTEXT_APP_FOCUS) {
432             this.toFront();
433         }
434     }
435
436 }
Popular Tags