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.image;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Dimension;
022: import java.awt.Image;
023: import java.awt.Toolkit;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.FocusAdapter;
026: import java.awt.event.FocusEvent;
027: import java.awt.event.MouseAdapter;
028: import java.awt.event.MouseEvent;
029: import java.io.File;
030: import java.util.Iterator;
031: import java.util.Map;
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034:
035: import javax.swing.AbstractAction;
036: import javax.swing.Action;
037: import javax.swing.JFileChooser;
038: import javax.swing.JScrollPane;
039: import javax.swing.border.BevelBorder;
040:
041: import de.finix.contelligent.client.base.ComponentFactory;
042: import de.finix.contelligent.client.base.ComponentNotFoundException;
043: import de.finix.contelligent.client.base.resource.ContelligentBinaryResource;
044: import de.finix.contelligent.client.event.ResourceEventListener;
045: import de.finix.contelligent.client.gui.ContelligentAction;
046: import de.finix.contelligent.client.gui.file.FileEditor;
047: import de.finix.contelligent.client.gui.file.Utils;
048: import de.finix.contelligent.client.i18n.Resources;
049: import de.finix.contelligent.client.modules.preferences.PreferencesModule;
050:
051: public class ImageEditor extends FileEditor implements
052: ResourceEventListener {
053:
054: private static Logger logger = Logger.getLogger(ImageEditor.class
055: .getName());
056:
057: private UploadFileAction uploadImageAction = new UploadFileAction(
058: "upload_image_action");
059:
060: private ReloadImageAction reloadImageAction = new ReloadImageAction();
061:
062: private ScrollableImage scrollableImage;
063:
064: public void init() {
065: scrollableImage = new ScrollableImage();
066: scrollableImage.setOpaque(false);
067: setResourceComponent(scrollableImage);
068: update();
069:
070: setDnD(isEditable());
071:
072: addFocusListener(new FocusAdapter() {
073: public void focusGained(FocusEvent event) {
074: borderPane.setBorder(new BevelBorder(
075: BevelBorder.LOWERED));
076: getView().setActions(getActions());
077: }
078:
079: public void focusLost(FocusEvent event) {
080: borderPane.setBorder(defaultBorder);
081: }
082: });
083:
084: borderPane = new JScrollPane(scrollableImage);
085: ((JScrollPane) borderPane).getViewport().setOpaque(false);
086: borderPane.setOpaque(false);
087: defaultBorder = borderPane.getBorder();
088: borderPane.addMouseListener(new MouseAdapter() {
089: public void mousePressed(MouseEvent event) {
090: requestFocus();
091: }
092: });
093: add(borderPane, BorderLayout.CENTER);
094: setPreferredSize(new Dimension(100, 100));
095: defaultBackground = getBackground();
096: highlightBackground = defaultBackground.brighter();
097: }
098:
099: public void update(boolean updateResource) {
100: boolean transform = false;
101: Image image = null;
102: if (getGUI().isResourceInherited()) {
103: transform = true;
104: }
105: if (!getGUI().isResourceDefined()) {
106: scrollableImage.setBorder(UNDEFINED_BORDER);
107: } else {
108: scrollableImage.setBorder(null);
109: ContelligentBinaryResource resource = (ContelligentBinaryResource) getGUI()
110: .getResource();
111: if (resource.getReferencedFile() == null) {
112: // the resource was not uploaded, so get it from server...
113: // build parameter map...
114: StringBuffer buffer = new StringBuffer(256);
115: if (getGUI().getResourceCategoryMap() != null) {
116: Iterator i = getGUI().getResourceCategoryMap()
117: .entrySet().iterator();
118: boolean firstParameter = true;
119: while (i.hasNext()) {
120: Map.Entry entry = (Map.Entry) i.next();
121: String categoryName = (String) entry.getKey();
122: String categoryValue = (String) entry
123: .getValue();
124: if (firstParameter) {
125: buffer.append('?');
126: firstParameter = false;
127: } else {
128: buffer.append('&');
129: }
130: buffer.append(categoryName).append('=').append(
131: categoryValue);
132: }
133: }
134: image = Resources.loadTemporaryImage(getComponent()
135: .getPath()
136: + ".bin" + buffer.toString());
137: } else {
138: image = Toolkit.getDefaultToolkit().createImage(
139: resource.getSource());
140: }
141: }
142: if (image != null) {
143: scrollableImage.setTransform(transform);
144: try {
145: scrollableImage.setImage(image);
146: } catch (Exception e) {
147: scrollableImage.setBorder(UNKNOWN_BORDER);
148: scrollableImage.setImage(Resources.gifIcon.getImage());
149: }
150: scrollableImage.revalidate();
151: scrollableImage.repaint();
152: }
153: }
154:
155: public boolean isScalable() {
156: return true;
157: }
158:
159: public Action[] getActions() {
160: if (isEditable()) {
161: return new Action[] { uploadImageAction, reloadImageAction };
162: } else {
163: return new Action[] {};
164: }
165: }
166:
167: protected void prepareFileChooser(JFileChooser fc) {
168: String fileDirectory = PreferencesModule
169: .getDirectoryPreferences().get(
170: PreferencesModule.IMAGE_DIRECTORY,
171: PreferencesModule.DEFAULT_IMAGE_DIRECTORY);
172: fc.setCurrentDirectory(new File(fileDirectory));
173: fc.addChoosableFileFilter(new ImageFilter());
174: fc.setFileView(new ImageFileView());
175: fc.setAccessory(new ImagePreview(fc));
176: }
177:
178: protected void evalFileChooser(JFileChooser fc) {
179: PreferencesModule.getDirectoryPreferences().put(
180: PreferencesModule.IMAGE_DIRECTORY,
181: fc.getCurrentDirectory().getAbsolutePath());
182: }
183:
184: protected String[] getSupportedMimeTypes() {
185: return Utils.BASIC_IMAGE_MIME_TYPES;
186: }
187:
188: public void rollback() {
189: // For an image, just always reload on rollback
190: try {
191: ComponentFactory.getInstance().reload(getComponent());
192: } catch (ComponentNotFoundException cnfe) {
193: logger.log(Level.SEVERE, "Could not rollback: ", cnfe);
194: }
195: update();
196: }
197:
198: private final class ReloadImageAction extends AbstractAction
199: implements ContelligentAction {
200: public ReloadImageAction() {
201: super ("reload_image_action", Resources.reloadImageIcon);
202: putValue(ROLLOVER_ICON, Resources.reloadImageIconRollOver);
203: putValue(TYPE, PUSH_ACTION);
204: putValue(ACTION_TYPE, EDIT_ACTION);
205: putValue(MENU_TARGET, MENU);
206: putValue(BUTTON_TARGET, TOOLBAR);
207: }
208:
209: public void actionPerformed(ActionEvent e) {
210: rollback();
211: update();
212: }
213: }
214: }
|