001:/*
002: * Copyright (c) 2000, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021:*/
022:
023:package org.skunk.dav.client.gui;
024:
025:import java.awt.event.MouseAdapter;
026:import java.awt.event.MouseEvent;
027:import java.awt.Component;
028:import java.awt.Point;
029:import javax.swing.JMenuItem;
030:import javax.swing.JPopupMenu;
031:import javax.swing.JSeparator;
032:import javax.swing.JTable;
033:import javax.swing.SwingUtilities;
034:import javax.swing.table.TableModel;
035:import org.skunk.assert.Assertion;
036:import org.skunk.dav.client.DAVFile;
037:import org.skunk.dav.client.Lock;
038:import org.skunk.dav.client.gui.action.CopyAction;
039:import org.skunk.dav.client.gui.action.DeleteAction;
040:import org.skunk.dav.client.gui.action.DownloadAction;
041:import org.skunk.dav.client.gui.action.LockAction;
042:import org.skunk.dav.client.gui.action.MoveAction;
043:import org.skunk.dav.client.gui.action.NewCollectionAction;
044:import org.skunk.dav.client.gui.action.NewFileAction;
045:import org.skunk.dav.client.gui.action.OpenAction;
046:import org.skunk.dav.client.gui.action.PropertiesAction;
047:import org.skunk.dav.client.gui.action.RefreshAction;
048:import org.skunk.dav.client.gui.action.RenameAction;
049:import org.skunk.dav.client.gui.action.StealLockAction;
050:import org.skunk.dav.client.gui.action.UnlockAction;
051:import org.skunk.dav.client.gui.action.UploadAction;
052:import org.skunk.trace.Debug;
053:
054:/**
055: * this listens for mouse events on both the Explorer's table
056: * and the scrollpane that contains it.
057: */
058:public class TableMouser extends MouseAdapter
059:{
060: private Explorer ex;
061:
062: //the six possible relevant lock states for a file
063: private static final int UNLOCKED=0;
064: private static final int OWN_SHARED_LOCK=1;
065: private static final int OTHER_SHARED_LOCK=2;
066: private static final int OWN_EXCLUSIVE_LOCK=3;
067: private static final int OTHER_STEALABLE_EXCLUSIVE_LOCK=4;
068: private static final int OTHER_UNSTEALABLE_EXCLUSIVE_LOCK=5;
069:
070:
071: public TableMouser(Explorer ex)
072: {
073: super ();
074: this .ex=ex;
075: }
076:
077: public void mousePressed(MouseEvent me)
078: {
079: if (SwingUtilities.isRightMouseButton(me)
080: && ex.getSelectedNode()!=null)
081: handlePopup(me);
082: }
083:
084:
085: public void mouseClicked(MouseEvent me)
086: {
087: if ((me.getSource() instanceof JTable)
088: && me.getClickCount()>1)
089: {
090: handleDoubleClick(me);
091: }
092: }
093:
094: private DAVFile getDAVFileFor(MouseEvent me)
095: {
096: Object source=me.getSource();
097: Assertion.assert((source instanceof JTable),
098: "source is a JTable");
099: JTable table=(JTable) source;
100: Point p=new Point(me.getX(), me.getY());
101: int row=table.rowAtPoint(p);
102: TableModel tm=table.getModel();
103: Assertion.assert((tm instanceof DAVTableModel),
104: "TableModel is a DAVTableModel");
105: DAVTableModel dtm=(DAVTableModel) tm;
106: DAVFile file=dtm.getDAVFile(row);
107: return file;
108: }
109:
110: private void handlePopup(MouseEvent me)
111: {
112: Object sourceObj=me.getSource();
113: JPopupMenu popper;
114: if (sourceObj instanceof JTable)
115: {
116: DAVFile file=getDAVFileFor(me);
117: Point p=new Point(me.getX(), me.getY());
118: popper=getPopup(file);
119: }
120: else popper=getNoFilePopup();
121: Assertion.assert((sourceObj instanceof Component),
122: "{0} is a Component",
123: new Object[] {sourceObj});
124: popper.show((Component) sourceObj, me.getX(), me.getY());
125: }
126:
127: private void handleDoubleClick(MouseEvent me)
128: {
129: DAVFile file=getDAVFileFor(me);
130: if (file.isCollection())
131: {
132: Debug.trace(this , Debug.DP3, "now opening collection " + file.getFileName());
133: ex.displayCollection(file.getFileName());
134: }
135: else
136: {
137: new OpenAction(ex, file).actionPerformed(null);
138: }
139: }
140:
141: private int getLockState(DAVFile file)
142: {
143: Assertion.assert((file!=null), "file is not null");
144: if (file.isLocked())
145: {
146: String lockOwner=file.getExclusiveLockOwner();
147: ServerData sd=ex.getSelectedConnection();
148: String localLockOwner=sd.getOwner();
149: if (localLockOwner==null)
150: localLockOwner=sd.getUsername();
151: if (file.isExclusiveLocked())
152: {
153: if (lockOwner!=null && lockOwner.equals(localLockOwner))
154: {
155: return OWN_EXCLUSIVE_LOCK;
156: }
157: else
158: {
159: if (sd.getPermitsLockStealing())
160: {
161: return OTHER_STEALABLE_EXCLUSIVE_LOCK;
162: }
163: else
164: {
165: return OTHER_UNSTEALABLE_EXCLUSIVE_LOCK;
166: }
167: }
168: }
169: else
170: {
171: Lock sharedLock=file.getLock(localLockOwner);
172: if (sharedLock!=null)
173: {
174: return OWN_SHARED_LOCK;
175: }
176: else
177: {
178: return OTHER_SHARED_LOCK;
179: }
180: }
181: }
182: else return UNLOCKED;
183: }
184:
185: private JPopupMenu getPopup(DAVFile file)
186: {
187: if (file==null)
188: return getNoFilePopup();
189: int lockState=getLockState(file);
190: JPopupMenu popper=new JPopupMenu();
191: /*
192: Open
193: Copy...
194: Delete
195: Move...
196: Rename
197: Lock/Unlock/Steal Lock
198: Upload
199: Download
200: Properties
201: */
202: JMenuItem handleItem=new JMenuItem();
203: handleItem.setEnabled(false);
204: popper.add(handleItem);
205: JMenuItem temp=popper.add(new OpenAction(ex, file));
206: temp.setText(ResourceManager.getMessage(ResourceManager.OPEN_KEY));
207: temp=popper.add(new CopyAction(ex, file));
208: temp.setText(ResourceManager.getMessage(ResourceManager.COPY_KEY));
209: temp=popper.add(new DeleteAction(ex, file.getFileName()));
210: //if file is locked, disable delete action, but still display it
211: temp.setText(ResourceManager.getMessage(ResourceManager.DELETE_KEY));
212: if (lockState!=UNLOCKED) temp.setEnabled(false);
213:
214: temp=popper.add(new MoveAction(ex, file));
215: temp.setText(ResourceManager.getMessage(ResourceManager.MOVE_KEY));
216: if (lockState==OTHER_UNSTEALABLE_EXCLUSIVE_LOCK) temp.setEnabled(false);
217:
218: temp=popper.add(new RenameAction(ex, file));
219: temp.setText(ResourceManager.getMessage(ResourceManager.RENAME_KEY));
220: if (lockState==OTHER_UNSTEALABLE_EXCLUSIVE_LOCK) temp.setEnabled(false);
221:
222: /*
223: if exclusive:
224: check lock owner and compare with the owner/username in serverdata;
225: if owner is not same:
226: show Steal Lock;
227: else:
228: show Unlock.
229: else:
230: if there is a shared lock matching the current principal:
231: include an Unlock
232: else:
233: include a Lock
234: */
235:
236: if (lockState==OWN_EXCLUSIVE_LOCK || lockState==OWN_SHARED_LOCK)
237: {
238: //principals match
239: popper.add(new UnlockAction(ex, file))
240: .setText(ResourceManager.getMessage(ResourceManager.UNLOCK_KEY));
241: }
242: else if (lockState==OTHER_STEALABLE_EXCLUSIVE_LOCK)
243: {
244: temp=popper.add(new StealLockAction(ex, file));
245: temp.setText(ResourceManager.getMessage(ResourceManager.STEAL_LOCK_KEY));
246: }
247: else if (lockState==UNLOCKED || lockState==OTHER_SHARED_LOCK)
248: {
249: popper.add(new LockAction(ex, file))
250: .setText(ResourceManager.getMessage(ResourceManager.LOCK_KEY));
251: }
252: if (!file.isCollection())
253: {
254: popper.add(new JSeparator());
255: temp=popper.add(new UploadAction(ex, file));
256: temp.setText(ResourceManager.getMessage(ResourceManager.UPLOAD_KEY));
257: if (lockState==OTHER_UNSTEALABLE_EXCLUSIVE_LOCK) temp.setEnabled(false);
258: popper.add(new DownloadAction(ex, file))
259: .setText(ResourceManager.getMessage(ResourceManager.DOWNLOAD_KEY));
260: }
261: popper.add(new JSeparator());
262: popper.add(new PropertiesAction(ex, file))
263: .setText(ResourceManager.getMessage(ResourceManager.PROPERTIES_KEY));
264: return popper;
265: }
266:
267: private JPopupMenu getNoFilePopup()
268: {
269: JPopupMenu popper=new JPopupMenu();
270: /*
271: Refresh
272: New File
273: New Directory
274: */
275: JMenuItem handleItem=new JMenuItem();
276: handleItem.setEnabled(false);
277: popper.add(handleItem);
278: String label=ResourceManager.getMessage(ResourceManager.NEW_FILE_KEY);
279: popper.add(new NewFileAction(ex)).setText(label);
280: label=ResourceManager.getMessage(ResourceManager.NEW_COLLECTION_KEY);
281: popper.add(new NewCollectionAction(ex)).setText(label);
282: label=ResourceManager.getMessage(ResourceManager.REFRESH_KEY);
283: popper.add(new RefreshAction(ex)).setText(label);
284: popper.add(new JSeparator());
285: label=ResourceManager.getMessage(ResourceManager.PROPERTIES_KEY);
286: popper.add(new PropertiesAction(ex)).setText(label);
287: return popper;
288: }
289:}
290:
291:/* $Log: TableMouser.java,v $
292:/* Revision 1.16 2000/12/19 22:36:05 smulloni
293:/* adjustments to preamble.
294:/*
295:/* Revision 1.15 2000/12/04 23:51:16 smulloni
296:/* added ImageViewer; fixed word in SimpleTextEditor
297:/*
298:/* Revision 1.14 2000/12/03 23:53:26 smulloni
299:/* added license and copyright preamble to java files.
300:/*
301:/* Revision 1.13 2000/11/22 00:11:28 smullyan
302:/* editor now locks and unlocks, more or less appropriately.
303:/*
304:/* Revision 1.12 2000/11/15 19:45:36 smullyan
305:/* beginning of revamp of application to include multiple buffers.
306:/*
307:/* Revision 1.11 2000/11/13 04:56:42 smullyan
308:/* improved the organization of the code, which had become quasi-monstrous.
309:/*
310:/* Revision 1.10 2000/11/10 22:40:07 smullyan
311:/* added icon to table for resource type; fixes to copy and move; disabling of
312:/* menu items that are inappropriate.
313:/*
314:/* Revision 1.9 2000/11/09 23:35:00 smullyan
315:/* log added to every Java file, with the help of python. Lock stealing
316:/* implemented, and treatment of locks made more robust.
317:/* */
|