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.action;
024:
025: import java.awt.event.ActionEvent;
026: import java.util.Iterator;
027: import javax.swing.AbstractAction;
028: import org.skunk.dav.client.DAVFile;
029: import org.skunk.dav.client.gui.Buffer;
030: import org.skunk.dav.client.gui.DAVTreeNode;
031: import org.skunk.dav.client.gui.Explorer;
032: import org.skunk.dav.client.gui.ExplorerApp;
033: import org.skunk.dav.client.gui.ServerData;
034: import org.skunk.dav.client.gui.View;
035: import org.skunk.dav.client.gui.editor.CannotLoadException;
036: import org.skunk.dav.client.gui.editor.DAVEditor;
037: import org.skunk.dav.client.gui.editor.DAVEditorFactory;
038: import org.skunk.trace.Debug;
039:
040: public class OpenAction extends AbstractAction {
041: private Explorer ex;
042: private DAVTreeNode node;
043: private DAVFile file;
044:
045: public OpenAction(Explorer ex) {
046: this (ex, null, null);
047: }
048:
049: public OpenAction(Explorer ex, DAVFile file) {
050: this (ex, null, file);
051: }
052:
053: public OpenAction(Explorer ex, DAVTreeNode node, DAVFile file) {
054: this .ex = ex;
055: this .node = node;
056: this .file = file;
057: }
058:
059: public void actionPerformed(ActionEvent ae) {
060: final DAVTreeNode localNode = (node == null) ? ex
061: .getSelectedNode() : node;
062:
063: if (localNode == null) {
064: Debug.trace(this , Debug.DP3, "null node in OpenAction");
065: return;
066: }
067: final DAVFile localFile = (file == null) ? ex
068: .getSelectedTableFile() : file;
069: if (localFile == null) {
070: Debug.trace(this , Debug.DP3, "null file in OpenAction");
071: return;
072: }
073: if (localFile.isCollection()) {
074: ex.displayCollection(localNode, localFile.getFileName());
075: } else {
076: //determine if this file is already being edited
077: View v = ExplorerApp.getViewForBuffer(ex);
078: for (Iterator it = v.getDockedBuffers(); it.hasNext();) {
079: Object o = it.next();
080: if (o instanceof DAVEditor) {
081: DAVEditor ed = (DAVEditor) o;
082: if (ed.getResourceName().equals(
083: localFile.getFullName())) {
084: v.focus(ed);
085: return;
086: }
087: }
088: }
089: Debug.trace(this , Debug.DP3,
090: "invoking associated editor for file "
091: + localFile.getName());
092: final Explorer finalEx = ex;
093: if (localFile.isLocked()) {
094: if (localFile.isExclusiveLocked()) {
095: String lockOwner = localFile
096: .getExclusiveLockOwner();
097: ServerData sd = ex.getConnection(localNode);
098: String localLockOwner = sd.getOwner();
099: if (localLockOwner == null)
100: localLockOwner = sd.getUsername();
101: openFileInEditor(localFile,
102: (lockOwner != null && lockOwner
103: .equals(localLockOwner)));
104: }
105: //currently no support for shared locks
106: else
107: openFileInEditor(localFile, false);
108: } else {
109: ex.lock(localNode, localFile, new Runnable() {
110: public void run() {
111: openFileInEditor(finalEx.getFile(localNode,
112: localFile.getFileName()), true);
113: }
114: });
115: }
116: }
117: }
118:
119: private void openFileInEditor(DAVFile file, boolean writeable) {
120: DAVEditor editor = DAVEditorFactory.getEditor(file);
121: editor.addEditListener(ex);
122: byte[] body = ex.get(file);
123: editor.setResourceBody(body);
124: try {
125: editor.load();
126: } catch (CannotLoadException canal) {
127: editor.removeEditListener(ex);
128: editor = null;
129: editor = DAVEditorFactory.getDefaultEditor(file);
130: editor.addEditListener(ex);
131: editor.setResourceBody(body);
132: try {
133: editor.load();
134: } catch (CannotLoadException canal2) {
135: //should not happen
136: Debug.trace(this , Debug.DP2, canal2);
137: }
138: }
139: editor.setWriteable(writeable);
140: View v = ExplorerApp.getViewForBuffer(ex);
141: if (v != null)
142: v.dock(editor);
143: }
144: }
145:
146: /* $Log: OpenAction.java,v $
147: /* Revision 1.15 2000/12/19 22:36:05 smulloni
148: /* adjustments to preamble.
149: /*
150: /* Revision 1.14 2000/12/08 05:50:30 smulloni
151: /* fixed MessageCatalogEditor. The spi features are now a special build option,
152: /* and editors are loaded through reflection.
153: /*
154: /* Revision 1.13 2000/12/04 23:51:16 smulloni
155: /* added ImageViewer; fixed word in SimpleTextEditor
156: /*
157: /* Revision 1.12 2000/12/03 23:53:26 smulloni
158: /* added license and copyright preamble to java files.
159: /*
160: /* Revision 1.11 2000/11/23 04:37:34 smullyan
161: /* editor and explorer now synch their files using the StateMonitor, which has
162: /* been improved significantly
163: /*
164: /* Revision 1.10 2000/11/22 00:11:29 smullyan
165: /* editor now locks and unlocks, more or less appropriately.
166: /*
167: /* Revision 1.9 2000/11/20 23:30:22 smullyan
168: /* more editor integration work.
169: /*
170: /* Revision 1.8 2000/11/18 04:36:05 smullyan
171: /* work on StateMonitor and related functionality.
172: /*
173: /* Revision 1.7 2000/11/16 23:16:47 smullyan
174: /* more preliminary work on editor integration
175: /*
176: /* Revision 1.6 2000/11/16 20:45:20 smullyan
177: /* the start of editor integration.
178: /*
179: /* Revision 1.5 2000/11/15 20:17:04 smullyan
180: /* added a Buffer interface, which is a wrapper around a displayable component.
181: /*
182: /* Revision 1.4 2000/11/15 19:45:37 smullyan
183: /* beginning of revamp of application to include multiple buffers.
184: /*
185: /* Revision 1.3 2000/11/09 23:35:04 smullyan
186: /* log added to every Java file, with the help of python. Lock stealing
187: /* implemented, and treatment of locks made more robust.
188: /* */
|