Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: medi/swing/dialog/SessionManagement.java


1   /*
2    * SessionManagement.java
3    *
4    * Created on 21 aprile 2002, 18.58
5       Medi - A media archiver. Archives media files for easy management.
6       Copyright (C) 2002-2003  Antonio Petrelli
7   
8       This program is free software; you can redistribute it and/or modify
9       it under the terms of the GNU General Public License as published by
10      the Free Software Foundation; either version 2 of the License, or
11      (at your option) any later version.
12  
13      This program is distributed in the hope that it will be useful,
14      but WITHOUT ANY WARRANTY; without even the implied warranty of
15      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16      GNU General Public License for more details.
17  
18      You should have received a copy of the GNU General Public License
19      along with this program; if not, write to the Free Software
20      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  
22      Contact me at: brenmcguire@users.sourceforge.net
23   */
24  
25  package medi.swing.dialog;
26  
27  import javatools.db.*;
28  import medi.db.AbstractProvider;
29  import medi.swing.tree.MediTreeNode;
30  import medi.swing.util.*;
31  
32  /**
33   * It is a frame to manage assignment of data to a session, or session position.
34   * @author Antonio Petrelli
35   * @version 0.3.0
36   */
37  public class SessionManagement extends javax.swing.JDialog {
38  
39      /** Adds an array of data to a session.
40       */    
41      public final int ADD_MODE = 1;
42      /** Moves a data array from a session to another session.
43       */    
44      public final int MOVE_MODE = 2;
45      /** Moves a session from a position to another.
46       */    
47      public final int MOVE_SESSION_MODE = 3;
48      
49      /** Creates new form SessionManagement
50       * @param parent The parent frame.
51       * @param modal The modal flag.
52       */
53      public SessionManagement(java.awt.Frame parent, boolean modal) {
54          super(parent, modal);
55          mediBundle = java.util.ResourceBundle.getBundle("res/MediBundle");
56          initComponents();
57          butSelect.setEnabled(false);
58      }
59  
60      /** Sets the provider to use.
61       * @param pPrv The provider to use.
62       */    
63      public void setProvider(AbstractProvider pPrv) {
64          prv = pPrv;
65      }
66  
67      /** Sets the mode (ADD_MODE, MOVE_MODE or MOVE_SESSION_MODE).
68       * @param pMode The mode.
69       */    
70      public void setMode(int pMode) {
71          mode = pMode;
72          switch (mode) {
73              case ADD_MODE:
74                  lblSessionSelection.setText(mediBundle.getString("Choose_a_directory_to_put_your_data"));
75                  break;
76              case MOVE_MODE:
77                  lblSessionSelection.setText(mediBundle.getString("Choose_a_directory_to_move_your_data_to"));
78                  break;
79              case MOVE_SESSION_MODE:
80                  lblSessionSelection.setText(mediBundle.getString("Choose_a_place_to_move_your_directory_to"));
81                  break;
82          }
83      }
84      
85      /** Shows this frame.
86       */    
87      public void show() {
88          try {
89              initTreeSession();
90          }
91          catch (DbException e) {
92              System.out.println(e.getMessage());
93          }
94          super.show();
95      }
96      
97      /** Sets the data array.
98       * @param pDataIDs The array of data IDs.
99       */    
100     public void setDataIDs(Long[] pDataIDs) {
101         dataIDs = pDataIDs;
102     }
103     
104     /** Sets the origin session.
105      * @param pVolumeID The origin volume ID.
106      * @param pSessionID The origin session ID.
107      */    
108     public void setOriginSession(Integer pVolumeID, Integer pSessionID) {
109         volumeID = pVolumeID;
110         sessionID = pSessionID;
111     }
112     
113     /** This method is called from within the constructor to
114      * initialize the form.
115      * WARNING: Do NOT modify this code. The content of this method is
116      * always regenerated by the Form Editor.
117      */
118     private void initComponents() {//GEN-BEGIN:initComponents
119         scpSession = new javax.swing.JScrollPane();
120         treeSession = new javax.swing.JTree();
121         panButtons = new javax.swing.JPanel();
122         butSelect = new javax.swing.JButton();
123         butCancel = new javax.swing.JButton();
124         lblSessionSelection = new javax.swing.JLabel();
125 
126         addWindowListener(new java.awt.event.WindowAdapter() {
127             public void windowClosing(java.awt.event.WindowEvent evt) {
128                 closeDialog(evt);
129             }
130         });
131 
132         scpSession.setPreferredSize(new java.awt.Dimension(400, 363));
133         treeSession.setToolTipText(mediBundle.getString("Volume/session_tree"));
134         treeSession.setShowsRootHandles(true);
135         treeSession.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
136             public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
137                 treeSessionValueChanged(evt);
138             }
139         });
140 
141         treeSession.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener() {
142             public void treeWillExpand(javax.swing.event.TreeExpansionEvent evt)
143             throws javax.swing.tree.ExpandVetoException {
144                 treeSessionTreeWillExpand(evt);
145             }
146             public void treeWillCollapse(javax.swing.event.TreeExpansionEvent evt)
147             throws javax.swing.tree.ExpandVetoException {
148                 treeSessionTreeWillCollapse(evt);
149             }
150         });
151 
152         scpSession.setViewportView(treeSession);
153 
154         getContentPane().add(scpSession, java.awt.BorderLayout.CENTER);
155 
156         butSelect.setText(mediBundle.getString("Select"));
157         butSelect.setToolTipText(mediBundle.getString("Choose_the_selected_session/volume"));
158         butSelect.addActionListener(new java.awt.event.ActionListener() {
159             public void actionPerformed(java.awt.event.ActionEvent evt) {
160                 butSelectActionPerformed(evt);
161             }
162         });
163 
164         panButtons.add(butSelect);
165 
166         butCancel.setText(mediBundle.getString("Cancel"));
167         butCancel.setToolTipText(mediBundle.getString("Cancel_the_operation"));
168         butCancel.addActionListener(new java.awt.event.ActionListener() {
169             public void actionPerformed(java.awt.event.ActionEvent evt) {
170                 butCancelActionPerformed(evt);
171             }
172         });
173 
174         panButtons.add(butCancel);
175 
176         getContentPane().add(panButtons, java.awt.BorderLayout.SOUTH);
177 
178         lblSessionSelection.setText(mediBundle.getString("This_label_will_change"));
179         lblSessionSelection.setPreferredSize(new java.awt.Dimension(157, 20));
180         getContentPane().add(lblSessionSelection, java.awt.BorderLayout.NORTH);
181 
182         pack();
183     }//GEN-END:initComponents
184 
185     private void butCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butCancelActionPerformed
186         // Add your handling code here:
187         setVisible(false);
188         dispose();
189     }//GEN-LAST:event_butCancelActionPerformed
190 
191     private void butSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butSelectActionPerformed
192         // Add your handling code here:
193         int result;
194         
195         result = javax.swing.JOptionPane.showConfirmDialog(null,
196             mediBundle.getString("Are_you_sure_to_select_this_session?"),
197             mediBundle.getString("Confirm_session_selection"), javax.swing.JOptionPane.YES_NO_OPTION,
198             javax.swing.JOptionPane.QUESTION_MESSAGE);
199         
200         if (result != javax.swing.JOptionPane.YES_OPTION)
201             return;
202         
203         try {
204             switch (mode) {
205                 case ADD_MODE:
206                     addLinkData2Session();
207                     break;
208                 case MOVE_MODE:
209                     moveLinkData2Session();
210                     break;
211                 case MOVE_SESSION_MODE:
212                     prv.moveLinkSessionToFatherSession(volumeID, sessionID,
213                         newVolumeID, newSessionID);
214                     break;
215                 default:
216             }
217         }
218         catch (DbException e) {
219             System.out.println(e.getMessage());
220         }
221         setVisible(false);
222         dispose();
223     }//GEN-LAST:event_butSelectActionPerformed
224 
225     private void treeSessionValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeSessionValueChanged
226         // Add your handling code here:
227         int index;
228         MediTreeNode node;
229         javax.swing.tree.TreePath tempPath;
230         Object[] tempIDs;
231         
232         tempPath = evt.getPath();
233         index = tempPath.getPathCount();
234         node = (MediTreeNode) tempPath.getLastPathComponent();
235         tempIDs = node.getID();
236         if (index >= 4) {
237             tempIDs = node.getID();
238             newVolumeID = (Integer) tempIDs[0];
239             newSessionID = (Integer) tempIDs[1];
240             if (mode == MOVE_SESSION_MODE && volumeID.equals(newVolumeID) &&
241                 sessionID.equals(newSessionID))
242                     butSelect.setEnabled(false);
243             else
244                 butSelect.setEnabled(true);
245         }
246         else if (index == 3 && mode == MOVE_SESSION_MODE) {
247             tempIDs = node.getID();
248             newVolumeID = (Integer) tempIDs[0];
249             newSessionID = null;
250             butSelect.setEnabled(true);
251         }
252         else {
253             volumeID = null;
254             sessionID = null;
255             butSelect.setEnabled(false);
256         }
257     }//GEN-LAST:event_treeSessionValueChanged
258 
259     private void treeSessionTreeWillCollapse(javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {//GEN-FIRST:event_treeSessionTreeWillCollapse
260         // Add your handling code here:
261        ((MediTreeNode) evt.getPath().getLastPathComponent()).setExpanded(false);
262     }//GEN-LAST:event_treeSessionTreeWillCollapse
263 
264     private void treeSessionTreeWillExpand(javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {//GEN-FIRST:event_treeSessionTreeWillExpand
265         // Add your handling code here:
266         Object[] tempIDs;
267         String type;
268         int pathCount;
269         MediTreeNode tempNode;
270         javax.swing.tree.TreePath tempPath;
271         DbIterator rowIt;
272         
273         try {
274             tempPath = evt.getPath();
275             pathCount = tempPath.getPathCount();
276             tempNode = (MediTreeNode) tempPath.getLastPathComponent();
277             
278             if (tempNode.isExpanded())
279                 return;
280             
281             switch (pathCount) {
282                 case 2:
283                     tempIDs = tempNode.getID();
284                     rowIt = prv.getVolumes((Integer) tempIDs[0], null).iterator();
285                     TreeUtils.fillTree(tempNode, rowIt, volumeIndexes, volumeIDIndexes, mediBundle.getString("Volume"), ": ", true);
286 //                    ((javax.swing.tree.DefaultTreeModel) treeSession.getModel()).reload();
287                     break;
288                 case 3:
289                     tempIDs = tempNode.getID();
290                     rowIt = prv.getSessionsSorted((Integer) tempIDs[0], null, null).iterator();
291                     TreeUtils.fillTree(tempNode, rowIt, sessionIndexes, sessionIDIndexes, mediBundle.getString("Session"), ": ", true);
292 //                    ((javax.swing.tree.DefaultTreeModel) treeSession.getModel()).reload();
293                     break;
294                 default:
295                     if (pathCount >= 4) {
296                         tempIDs = tempNode.getID();
297                         rowIt = prv.getSessionsSorted((Integer) tempIDs[0], null, (Integer) tempIDs[1]).iterator();
298                         TreeUtils.fillTree(tempNode, rowIt, sessionIndexes, sessionIDIndexes, mediBundle.getString("Session"), ": ", true);
299 //                        ((javax.swing.tree.DefaultTreeModel) treeSession.getModel()).reload();
300                     }
301             }
302             tempNode.setExpanded(true);
303         }
304         catch (DbException e) {
305             System.out.println(e.getMessage());
306         }
307     }//GEN-LAST:event_treeSessionTreeWillExpand
308 
309     /** Closes the dialog */
310     private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
311         setVisible(false);
312         dispose();
313     }//GEN-LAST:event_closeDialog
314 
315     // Variables declaration - do not modify//GEN-BEGIN:variables
316     private javax.swing.JTree treeSession;
317     private javax.swing.JButton butCancel;
318     private javax.swing.JButton butSelect;
319     private javax.swing.JScrollPane scpSession;
320     private javax.swing.JLabel lblSessionSelection;
321     private javax.swing.JPanel panButtons;
322     // End of variables declaration//GEN-END:variables
323 
324     private AbstractProvider prv;
325     private int mode;
326     private Long[] dataIDs;
327     private Integer volumeID, sessionID;
328     private Integer newVolumeID, newSessionID;
329     private MediTreeNode containerRoot;
330     private DbResult containerRes;
331     private java.util.ResourceBundle mediBundle;
332 
333     private int[] containerIndexes = {0, 1};
334     private int[] containerIDIndexes = {0};
335     private int[] volumeIndexes = {0, 2};
336     private int[] volumeIDIndexes = {0};
337     private int[] sessionIndexes = {1, 4};
338     private int[] sessionIDIndexes = {0, 1};
339     
340     private void initTreeSession() throws DbException {
341         DbIterator rowIt;
342         MediTreeNode tempRoot;
343         
344         containerRoot = new MediTreeNode(mediBundle.getString("Containers"));
345         ((javax.swing.tree.DefaultTreeModel) treeSession.getModel()).setRoot(containerRoot);
346         containerRes = prv.getContainers(null);
347         rowIt = containerRes.iterator();
348         tempRoot = (MediTreeNode)
349             ((javax.swing.tree.DefaultTreeModel) treeSession.getModel()).getRoot();
350         TreeUtils.fillTree(tempRoot, rowIt, containerIndexes, containerIDIndexes, mediBundle.getString("Container"), ": ", true);
351     }
352     
353     private void addLinkData2Session() throws DbException {
354         int i, numValues;
355         MediTreeNode tempNode;
356         
357         numValues = dataIDs.length;
358         for (i=0; i < numValues; i++) {
359             prv.linkDataToSession(dataIDs[i], newVolumeID, newSessionID);
360         }
361     }
362     
363     private void moveLinkData2Session() throws DbException {
364         int i, numValues;
365         MediTreeNode tempNode;
366         
367         numValues = dataIDs.length;
368         for (i=0; i < numValues; i++) {
369             prv.moveLinkDataToSession(dataIDs[i], volumeID, sessionID, 
370                 newVolumeID, newSessionID);
371         }
372     }
373 }