001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.gui.explorer;
019:
020: import java.awt.event.ActionEvent;
021: import java.io.File;
022:
023: import javax.swing.AbstractAction;
024: import javax.swing.JFileChooser;
025: import javax.swing.JOptionPane;
026: import javax.swing.tree.DefaultMutableTreeNode;
027:
028: import de.finix.contelligent.client.ContelligentClient;
029: import de.finix.contelligent.client.base.ComponentNotFoundException;
030: import de.finix.contelligent.client.base.ComponentReference;
031: import de.finix.contelligent.client.base.ContelligentComponent;
032: import de.finix.contelligent.client.gui.ContelligentAction;
033: import de.finix.contelligent.client.gui.file.Utils;
034: import de.finix.contelligent.client.i18n.Resources;
035: import de.finix.contelligent.client.remote.ActionResult;
036: import de.finix.contelligent.client.remote.Actions;
037: import de.finix.contelligent.client.remote.RemoteActionException;
038: import de.finix.contelligent.client.util.ExceptionDialog;
039:
040: public class ImportAction extends AbstractAction implements
041: ContelligentAction {
042: /**
043: *
044: */
045: private final ExplorerEditor editor;
046:
047: public ImportAction(ExplorerEditor editor) {
048: super ("import_action", Resources.uploadIcon);
049: this .editor = editor;
050: putValue(ROLLOVER_ICON, Resources.uploadIconRollOver);
051: putValue(TYPE, PUSH_ACTION);
052: putValue(ACTION_TYPE, FILE_ACTION);
053: putValue(MENU_TARGET, MENU);
054: putValue(BUTTON_TARGET, NO_BUTTON);
055: }
056:
057: public void actionPerformed(ActionEvent e) {
058: DefaultMutableTreeNode node = (DefaultMutableTreeNode) this .editor.tree
059: .getLastSelectedPathComponent();
060: if (node != null
061: && node.getUserObject() instanceof ComponentReference) {
062: try {
063: ContelligentComponent component = ((ComponentReference) node
064: .getUserObject()).getComponent();
065:
066: final JFileChooser fc = new JFileChooser();
067: fc.setMultiSelectionEnabled(true);
068: if (fc.showDialog(this .editor, Resources
069: .getLocalString("upload")) == JFileChooser.APPROVE_OPTION) {
070: File[] files = fc.getSelectedFiles();
071: for (int i = 0; i < files.length; i++) {
072: File file = files[i];
073: if (!file.exists()) {
074: // We got bad data from the file chooser
075: JOptionPane
076: .showMessageDialog(
077: ContelligentClient
078: .getFrame(),
079: Resources
080: .getLocalString("file_not_found"));
081: return;
082: }
083: String extension = Utils.getExtension(file);
084: if (extension == null) {
085: extension = "";
086: }
087: String mimeType = Utils
088: .getMimeForExtension(extension);
089: if (mimeType.equals("")) {
090: // Safe default for unknown mime type
091: mimeType = "application/octet-stream";
092: }
093: String cleanName = file.getName().replace('/',
094: '_').replace('\u00C4', 'A').replace(
095: '\u00D6', 'O').replace('\u00DC', 'U')
096: .replace('\u00E4', 'a').replace(
097: '\u00F6', 'o').replace(
098: '\u00FC', 'u');
099: ActionResult result = Actions.upload(editor
100: .getView().getEnvironment(), component
101: .getPath()
102: + "/" + cleanName, file
103: .getAbsolutePath(), false, mimeType,
104: extension);
105: if (result
106: .hasError("contelligent.core.component.alreadyExists")) {
107: int selected = JOptionPane
108: .showConfirmDialog(
109: this .editor.getParent(),
110: Resources
111: .getLocalString(
112: "target_component_exists_override",
113: new String[] { file
114: .getName() }),
115: Resources
116: .getLocalString("component_exists"),
117: JOptionPane.YES_NO_CANCEL_OPTION);
118: if (selected == JOptionPane.YES_OPTION) {
119: // OK, try again with overwrite flag set to true
120: result = Actions.upload(editor
121: .getView().getEnvironment(),
122: component.getPath() + "/"
123: + file.getName(), file
124: .getAbsolutePath(),
125: true, mimeType, extension);
126: result.showErrors();
127: } else if (selected == JOptionPane.CANCEL_OPTION) {
128: // User wants to cancel, so skip all files still
129: // queued
130: return;
131: }
132: } else {
133: result.showErrors();
134: }
135: }
136: }
137: } catch (ComponentNotFoundException cnfe) {
138: ExceptionDialog.show(cnfe);
139: } catch (RemoteActionException rae) {
140: ExceptionDialog.show(rae);
141: }
142: }
143: }
144: }
|