package de.grapheditor;
import research.*;
import research.util.GlobalFunction;
import research.util.colorchooser.DefaultRGBAChooserPanel;
import research.util.colorchooser.DefaultRGBAChooserPanel;
import research.util.colorchooser.DefaultHSBAChooserPanel;
import research.util.colorchooser.DefaultSwatchChooserPanel;
import de.reuse.DrawingEditorTabbedPane;
import de.reuse.GroupTreeMap;
import de.reuse.Context;
import de.reuse.ContextImplementation;
import javax.swing.*;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.dnd.*;
import java.awt.*;
import java.awt.image.FilteredImageSource;
import java.awt.image.RGBImageFilter;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.Vector;
import java.util.Iterator;
import java.io.*;
import org.freehep.util.export.ExportDialog;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import Acme.JPM.Encoders.GifEncoder;
/**
* Created by IntelliJ IDEA.
* User: zhangwei
* Date: 2004-7-4
* Time: 1:07:19
* To change this template use File | Settings | File Templates.
*/
class Implementation implements Interface {
protected final Context context = new ContextImplementation();
protected DropTarget dropTarget = null;
protected SaturnToolBar toolBar = null;
protected DrawingEditorTabbedPane editorArea = null;
protected JSpinner scaler = null;
protected JPopupMenu editorAreaPopupMenu = null;
protected AddViewAction addViewAction = null;
protected CloseViewAction closeViewAction = null;
protected OpenViewAction openViewAction = null;
protected RemoveViewAction removeViewAction = null;
//��˶���
private EditorAction leftAlignAction = null;
//��ֱ����
private EditorAction vertAlignAction = null;
//�Ҷ˶���
private EditorAction rightAlignAction = null;
//���˶���
private EditorAction topAlignAction = null;
//ˮƽ����
private EditorAction horiAlignAction = null;
//�˶���
private EditorAction botAlignAction = null;
//����ֲ�
private EditorAction vertDistAction = null;
//����ֲ�
private EditorAction horiDistAction = null;
//protected FigureDeleteAction figureDeleteAction = null;
//protected FigureCopyAction figureCopyAction = null;
//protected FigureCutAction figureCutAction = null;
//protected FigurePasteAction figurePasteAction = null;
protected SelectAllAction selectAllAction = null;
protected ToggleSelectAction toggleSelectAction = null;
protected SendToBackAction sendToBackAction = null;
protected SendToFrontAction sendToFrontAction = null;
protected ExportViewAction exportViewAction = null;
protected ChangeColorAction changeFillColorAction = null;
protected ChangeColorAction changeFrameColorAction = null;
protected ChangeColorAction changeTextColorAction = null;
protected DropTargetListener dropTargetListener = null;
protected ActionMap actionMap = null;
protected UIImplementation graphicalEditorUI = null;
protected final CurrentViewFigureSelectionChangedListener currentViewFigureSelectionChangedListener = new CurrentViewFigureSelectionChangedListener();
protected final ContextChangeListener contextChangeListener = new ContextChangeListener();
protected final ViewAddedOrRemovedListener viewAddedOrRemovedListener = new ViewAddedOrRemovedListener();
protected final PopupMenuTriggeredListener popupMenuTriggeredListener = new PopupMenuTriggeredListener();
protected final GroupTreeMap idToFigureMap = new GroupTreeMap();
private final static ExportDialog exportDialog = new ExportDialog();
private final static DefaultRGBAChooserPanel rgbaChooser = new DefaultRGBAChooserPanel();
private final static DefaultHSBAChooserPanel hsbaChooser = new DefaultHSBAChooserPanel();
private final static DefaultSwatchChooserPanel dsChooser = new DefaultSwatchChooserPanel();
public Implementation() {
editorArea = new DrawingEditorTabbedPane();
editorArea.addPropertyChangeListener(new DrawingEditorPropertyChangeListener());
editorArea.addPropertyChangeListener(DrawingEditor.CURRENT_VIEW_CHANGED, new CurrentViewChangeListener());
editorArea.addPropertyChangeListener(DrawingEditor.VIEW_ADDED, viewAddedOrRemovedListener);
editorArea.addPropertyChangeListener(DrawingEditor.VIEW_REMOVED, viewAddedOrRemovedListener);
editorArea.addPropertyChangeListener(DrawingEditor.POPUP_MENU_TRIGGERED, popupMenuTriggeredListener);
toolBar = new SaturnToolBar();
toolBar.getController().setDrawingEditor(editorArea);
getContext().putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
getContext().putValue(ConstantDefinition.ID_TO_FIGURE_MAP, idToFigureMap);
getContext().addContextChangeListener(ConstantDefinition.DROP_TARGET, contextChangeListener);
scaler = new JSpinner(new SpinnerNumberModel(100, 10, 600, 5));
scaler.setEnabled(false);
scaler.setToolTipText("������ǰ��ͼ��ʾ����");
scaler.setMaximumSize(scaler.getPreferredSize());
scaler.setAlignmentX(0.5f);
scaler.setAlignmentY(0.5f);
scaler.getModel().addChangeListener(new SpinnerValueChangeListener());
editorAreaPopupMenu = new JPopupMenu();
addViewAction = new AddViewAction();
addViewAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
closeViewAction = new CloseViewAction();
closeViewAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
openViewAction = new OpenViewAction();
openViewAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
removeViewAction = new RemoveViewAction();
removeViewAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
selectAllAction = new SelectAllAction();
selectAllAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
toggleSelectAction = new ToggleSelectAction();
toggleSelectAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
sendToBackAction = new SendToBackAction();
sendToBackAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
sendToFrontAction = new SendToFrontAction();
sendToFrontAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
exportViewAction = new ExportViewAction();
exportViewAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
changeFillColorAction = new ChangeColorAction("�����ɫ", "fillColor");
changeFillColorAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
changeFrameColorAction = new ChangeColorAction("�߿���ɫ", "frameColor");
changeFrameColorAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
changeTextColorAction = new ChangeColorAction("������ɫ", "textColor");
changeTextColorAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
leftAlignAction = new LeftAlignAction();
leftAlignAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
vertAlignAction = new VertAlignAction();
vertAlignAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
rightAlignAction = new RightAlignAction();
rightAlignAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
topAlignAction = new TopAlignAction();
topAlignAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
horiAlignAction = new HoriAlignAction();
horiAlignAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
botAlignAction = new BotAlignAction();
botAlignAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
vertDistAction = new VertDistAction();
vertDistAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
horiDistAction = new HoriDistAction();
horiDistAction.putValue(ConstantDefinition.DRAWING_EDITOR, editorArea);
/**
figureDeleteAction = new FigureDeleteAction();
figureCopyAction = new FigureCopyAction();
figureCutAction = new FigureCutAction();
figurePasteAction = new FigurePasteAction();
figurePasteAsLinkAction = new FigurePasteAsLinkAction();
figureDeleteAction.putValue(ConstantDefinition.ID_TO_FIGURE_MAP, idToFigureMap);
figureCopyAction.putValue(ConstantDefinition.ID_TO_FIGURE_MAP, idToFigureMap);
figureCutAction.putValue(ConstantDefinition.ID_TO_FIGURE_MAP, idToFigureMap);
figurePasteAction.putValue(ConstantDefinition.ID_TO_FIGURE_MAP, idToFigureMap);
figurePasteAsLinkAction.putValue(ConstantDefinition.ID_TO_FIGURE_MAP, idToFigureMap);
**/
actionMap = new ActionMap();
actionMap.put(addViewAction.getValue(Action.NAME), addViewAction);
actionMap.put(closeViewAction.getValue(Action.NAME), closeViewAction);
actionMap.put(openViewAction.getValue(Action.NAME), openViewAction);
actionMap.put(removeViewAction.getValue(Action.NAME), removeViewAction);
actionMap.put(exportViewAction.getValue(Action.NAME), exportViewAction);
actionMap.put(ConstantDefinition.LEFT_ALIGN, leftAlignAction);
actionMap.put(ConstantDefinition.VERTICAL_ALIGN, vertAlignAction);
actionMap.put(ConstantDefinition.RIGHT_ALIGN, rightAlignAction);
actionMap.put(ConstantDefinition.TOP_ALIGN, topAlignAction);
actionMap.put(ConstantDefinition.HORIZONTAL_ALIGN, horiAlignAction);
actionMap.put(ConstantDefinition.BOTTOM_ALIGN, botAlignAction);
actionMap.put(ConstantDefinition.VERTICAL_DISTRIBUTE, vertDistAction);
actionMap.put(ConstantDefinition.HORIZONTAL_DISTRIBUTE, horiDistAction);
/**
actionMap.put(ConstantDefinition.FIGURE_DELETE_ACTION, figureDeleteAction);
actionMap.put(ConstantDefinition.FIGURE_COPY_ACTION, figureCopyAction);
actionMap.put(ConstantDefinition.FIGURE_CUT_ACTION, figureCutAction);
actionMap.put(ConstantDefinition.FIGURE_PASTE_ACTION, figurePasteAction);
actionMap.put(ConstantDefinition.FIGURE_PASTE_AS_LINK_ACTION, figurePasteAsLinkAction);
**/
actionMap.put(ConstantDefinition.FIGURE_SELECT_ALL_ACTION, selectAllAction);
actionMap.put(ConstantDefinition.FIGURE_TOGGLE_SELECT_ACTION, toggleSelectAction);
actionMap.put(ConstantDefinition.SEND_TO_BACK, sendToBackAction);
actionMap.put(ConstantDefinition.SEND_TO_FRONT, sendToFrontAction);
actionMap.put(ConstantDefinition.FILL_COLOR_CAHNGE_ACTION, changeFillColorAction);
actionMap.put(ConstantDefinition.FRAME_COLOR_CAHNGE_ACTION, changeFrameColorAction);
actionMap.put(ConstantDefinition.TEXT_COLOR_CAHNGE_ACTION, changeTextColorAction);
dropTargetListener = new InerDropTargetListener();
}
public Context getContext() {
return context;
}
protected class ContextChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
contextChange(e);
}
}
protected void contextChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if (ConstantDefinition.DROP_TARGET.equals(propertyName)) {
if (dropTarget != null) {
dropTarget.setComponent(null);
dropTarget.setActive(false);
}
dropTarget = (DropTarget) e.getNewValue();
if (dropTarget != null) {
dropTarget.setComponent((JComponent) editorArea.getCurrentView());
//dropTarget.setComponent(editorArea);
dropTarget.setActive(true);
}
}
}
protected class PopupMenuTriggeredListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if (DrawingEditor.POPUP_MENU_TRIGGERED.equals(propertyName)) {
int childCount = editorAreaPopupMenu.getComponentCount();
if (childCount > 0) {
Point p = (Point) e.getNewValue();
editorAreaPopupMenu.show((Component) e.getSource(), p.x, p.y);
}
}
}
}
protected class ViewAddedOrRemovedListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if (DrawingEditor.VIEW_ADDED.equals(propertyName)) {
DrawingView view = (DrawingView) e.getNewValue();
//����id��figure��ӳ���
if (idToFigureMap != null) {
FigureEnumeration fe = view.getDrawing().getFigures();
while (fe.hasMoreElements()) {
Figure figure = fe.nextFigure();
Object id = figure.getAttribute("id");
if (id == null) continue;
idToFigureMap.add(id, figure);
}
}
} else if (DrawingEditor.VIEW_REMOVED.equals(propertyName)) {
DrawingView view = (DrawingView) e.getOldValue();
//����id��figure��ӳ���
if (idToFigureMap != null) {
FigureEnumeration fe = view.getDrawing().getFigures();
while (fe.hasMoreElements()) {
Figure figure = fe.nextFigure();
Object id = figure.getAttribute("id");
if (id == null) continue;
idToFigureMap.remove(id, figure);
}
}
}
}
}
public UI getGraphicalEditorUI() {
if (graphicalEditorUI == null) {
graphicalEditorUI = new UIImplementation();
}
return graphicalEditorUI;
}
public ActionMap getActionMap() {
return actionMap;
}
public DropTargetListener getDropTargetListener() {
return dropTargetListener;
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
editorArea.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
editorArea.removePropertyChangeListener(listener);
}
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
editorArea.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
editorArea.removePropertyChangeListener(propertyName, listener);
}
public ArrayList getSelection() {
DrawingView currentView = editorArea.getCurrentView();
if (currentView == null) return null;
Vector selection = currentView.getSelection();
if ((selection == null) || (selection.size() <= 0)) return null;
ArrayList selectionList = new ArrayList(selection.size());
for (int i = 0; i < selection.size(); i++) {
Figure fig = (Figure) selection.get(i);
selectionList.add(fig.getAttribute("id"));
}
return selectionList;
}
protected class UIImplementation implements UI {
public JToolBar getToolBar() {
return toolBar;
}
public JComponent getEditorArea() {
return editorArea;
}
public JComponent getScaleComponent() {
return scaler;
}
public JPopupMenu getEditorAreaPopupMenu() {
return editorAreaPopupMenu;
}
}
protected class SpinnerValueChangeListener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
DrawingView currentView = editorArea.getCurrentView();
if (currentView != null) {
Integer number = (Integer) scaler.getModel().getValue();
double scale = number.intValue() / 100.0;
currentView.setScale(scale);
Dimension newSize = currentView.getPageSize();
newSize.width = (int) (newSize.width * scale);
newSize.height = (int) (newSize.height * scale);
((JComponent) currentView).setSize(newSize);
((JComponent) currentView).invalidate();
((JComponent) currentView).repaint();
}
}
}
protected class CurrentViewFigureSelectionChangedListener implements FigureSelectionListener {
public void figureSelectionChanged(DrawingView view) {
updateAlignActionsUI();
updateEditActionsUI();
}
}
protected class CurrentViewChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if (propertyName.equals(DrawingEditor.CURRENT_VIEW_CHANGED)) {
DrawingView oView = (DrawingView) e.getOldValue();
DrawingView cView = (DrawingView) e.getNewValue();
if ((oView == null) && (cView != null)) {
scaler.setEnabled(true);
scaler.validate();
}
if ((oView != null) && (cView == null)) {
scaler.setEnabled(false);
scaler.validate();
}
if (cView != null) {
int scale = (int) (cView.getScale() * 100);
scaler.setValue(new Integer(scale));
}
if (oView != null)
oView.removeFigureSelectionListener(currentViewFigureSelectionChangedListener);
if (cView != null)
cView.addFigureSelectionListener(currentViewFigureSelectionChangedListener);
if (dropTarget != null)
dropTarget.setComponent((JComponent) cView);
}
}
}
protected class DrawingEditorPropertyChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if (propertyName.equals(DrawingEditor.CURRENT_VIEW_CHANGED)) {
Object oldV = e.getOldValue();
Object newV = e.getNewValue();
if ((oldV == null) && (newV != null)) {
if (closeViewAction.isEnabled()) {
closeViewAction.firePropertyChange("enabled", Boolean.FALSE, Boolean.TRUE);
}
if (removeViewAction.isEnabled()) {
removeViewAction.firePropertyChange("enabled", Boolean.FALSE, Boolean.TRUE);
}
if (exportViewAction.isEnabled()) {
exportViewAction.firePropertyChange("enabled", Boolean.FALSE, Boolean.TRUE);
}
} else if ((oldV != null) && (newV == null)) {
if (!closeViewAction.isEnabled()) {
closeViewAction.firePropertyChange("enabled", Boolean.TRUE, Boolean.FALSE);
}
if (!removeViewAction.isEnabled()) {
removeViewAction.firePropertyChange("enabled", Boolean.TRUE, Boolean.FALSE);
}
if (!exportViewAction.isEnabled()) {
exportViewAction.firePropertyChange("enabled", Boolean.TRUE, Boolean.FALSE);
}
}
updateAlignActionsUI();
/**
figureDeleteAction.putValue(ConstantDefinition.DRAWING_VIEW, newV);
figureCopyAction.putValue(ConstantDefinition.DRAWING_VIEW, newV);
figureCutAction.putValue(ConstantDefinition.DRAWING_VIEW, newV);
figurePasteAction.putValue(ConstantDefinition.DRAWING_VIEW, newV);
figurePasteAsLinkAction.putValue(ConstantDefinition.DRAWING_VIEW, newV);
**/
updateEditActionsUI();
}
}
}
protected void updateAlignActionsUI() {
Boolean newState = Boolean.valueOf(leftAlignAction.isEnabled());
Boolean oldState = Boolean.valueOf(!newState.booleanValue());
leftAlignAction.firePropertyChange("enabled", oldState, newState);
vertAlignAction.firePropertyChange("enabled", oldState, newState);
rightAlignAction.firePropertyChange("enabled", oldState, newState);
topAlignAction.firePropertyChange("enabled", oldState, newState);
horiAlignAction.firePropertyChange("enabled", oldState, newState);
botAlignAction.firePropertyChange("enabled", oldState, newState);
newState = Boolean.valueOf(vertDistAction.isEnabled());
oldState = Boolean.valueOf(!newState.booleanValue());
vertDistAction.firePropertyChange("enabled", oldState, newState);
horiDistAction.firePropertyChange("enabled", oldState, newState);
}
protected void updateEditActionsUI() {
//Boolean newState = Boolean.valueOf(figureDeleteAction.isEnabled());
//Boolean oldState = Boolean.valueOf(!newState.booleanValue());
//figureDeleteAction.firePropertyChange("enabled", oldState, newState);
//figureCopyAction.firePropertyChange("enabled", oldState, newState);
//figureCutAction.firePropertyChange("enabled", oldState, newState);
{
Boolean newState = Boolean.valueOf(sendToBackAction.isEnabled());
Boolean oldState = Boolean.valueOf(!newState.booleanValue());
sendToBackAction.firePropertyChange("enabled", oldState, newState);
sendToFrontAction.firePropertyChange("enabled", oldState, newState);
}
//newState = Boolean.valueOf(figurePasteAction.isEnabled());
//oldState = Boolean.valueOf(!newState.booleanValue());
//figurePasteAction.firePropertyChange("enabled", oldState, newState);
//figurePasteAsLinkAction.firePropertyChange("enabled", oldState, newState);
{
Boolean newState = Boolean.valueOf(selectAllAction.isEnabled());
Boolean oldState = Boolean.valueOf(!newState.booleanValue());
selectAllAction.firePropertyChange("enabled", oldState, newState);
toggleSelectAction.firePropertyChange("enabled", oldState, newState);
}
}
protected class AddViewAction extends EditorAction {
protected boolean isDoing = false;
protected int count = 0;
public AddViewAction() {
this.putValue(Action.NAME, "addView");
isDoing = false;
}
public boolean isEnabled() {
if (!super.isEnabled()) return false;
String viewName = (String) this.getValue(ConstantDefinition.VIEW_NAME);
Drawing drawing = (Drawing) this.getValue(ConstantDefinition.DRAWING);
if ((viewName == null) && (drawing == null)) return false;
return true;
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
if (isDoing) return;
isDoing = true;
String viewName = (String) this.getValue(ConstantDefinition.VIEW_NAME);
Drawing drawing = (Drawing) this.getValue(ConstantDefinition.DRAWING);
SaturnDrawingView view = createDrawingView();
if (drawing == null) {
drawing = createDrawing();
drawing.setTitle(viewName);
}
view.setDrawing(drawing);
DrawingEditor drawingEditor = (DrawingEditor) getValue(ConstantDefinition.DRAWING_EDITOR);
drawingEditor.add(view);
this.putValue(ConstantDefinition.VIEW_NAME, null);
this.putValue(ConstantDefinition.DRAWING, null);
isDoing = false;
}
protected SaturnDrawingView createDrawingView() {
return new SaturnDrawingView(1600, 1200);
}
protected SaturnDrawing createDrawing() {
return new SaturnDrawing();
}
}
protected class CloseViewAction extends EditorAction {
public CloseViewAction() {
this.putValue(Action.NAME, "closeView");
}
public boolean isEnabled() {
if (!super.isEnabled()) return false;
if (editorArea.getCurrentView() == null) return false;
return true;
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
DrawingView currentView = editorArea.getCurrentView();
String viewName = currentView.getDrawing().getTitle();
editorArea.close(currentView);
this.putValue(ConstantDefinition.VIEW_NAME, viewName);
if (!openViewAction.isEnabled()) {
openViewAction.firePropertyChange("enabled", Boolean.FALSE, Boolean.TRUE);
}
}
}
protected class OpenViewAction extends EditorAction {
public OpenViewAction() {
this.putValue(Action.NAME, "openView");
}
public boolean isEnabled() {
if (!super.isEnabled()) return false;
if (editorArea.getClosedViews().length <= 0) return false;
return true;
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
DrawingView[] views = editorArea.getClosedViews();
for (int i = 0; i < views.length; i++) {
editorArea.open(views[i]);
}
}
}
protected class RemoveViewAction extends EditorAction {
public RemoveViewAction() {
this.putValue(Action.NAME, "removeView");
}
public boolean isEnabled() {
if (!super.isEnabled()) return false;
if (editorArea.getCurrentView() == null) return false;
return true;
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
DrawingView currentView = editorArea.getCurrentView();
String viewName = currentView.getDrawing().getTitle();
//selectAllAction.actionPerformed((ActionEvent) null);
//figureDeleteAction.actionPerformed((ActionEvent) null);
editorArea.remove(currentView);
this.putValue(ConstantDefinition.VIEW_NAME, viewName);
}
}
protected class ToggleSelectAction extends EditorAction {
public ToggleSelectAction() {
this.putValue(Action.NAME, "����ѡ��");
}
public boolean isEnabled() {
if (!super.isEnabled()) return false;
if (editorArea.getCurrentView() == null) return false;
return true;
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
DrawingView view = editorArea.getCurrentView();
FigureEnumeration fe = view.getDrawing().getFigures();
while (fe.hasMoreElements()) {
Figure f = fe.nextFigure();
view.toggleSelection(f);
}
view.repairDamage();
}
}
protected class SelectAllAction extends EditorAction {
public SelectAllAction() {
this.putValue(Action.NAME, "ȫ��ѡ��");
}
public boolean isEnabled() {
if (!super.isEnabled()) return false;
if (editorArea.getCurrentView() == null) return false;
return true;
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
DrawingView view = editorArea.getCurrentView();
view.addToSelectionAll(view.getDrawing().getFigures());
view.repairDamage();
}
}
protected class SendToBackAction extends EditorAction {
public SendToBackAction() {
this.putValue(Action.NAME, "�����ײ�");
}
public boolean isEnabled() {
if (!super.isEnabled()) return false;
if (editorArea.getCurrentView() == null) return false;
Vector selection = editorArea.getCurrentView().getSelection();
if (selection == null || selection.size() <= 0) return false;
return true;
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
DrawingView view = editorArea.getCurrentView();
FigureEnumeration fe = new ReverseFigureEnumerator(view.getSelectionZOrdered());
while (fe.hasMoreElements()) {
view.getDrawing().sendToBack(fe.nextFigure());
}
view.checkDamage();
}
}
protected class SendToFrontAction extends EditorAction {
public SendToFrontAction() {
this.putValue(Action.NAME, "��������");
}
public boolean isEnabled() {
if (!super.isEnabled()) return false;
if (editorArea.getCurrentView() == null) return false;
Vector selection = editorArea.getCurrentView().getSelection();
if (selection == null || selection.size() <= 0) return false;
return true;
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
DrawingView view = editorArea.getCurrentView();
FigureEnumeration fe = new ReverseFigureEnumerator(view.getSelectionZOrdered());
while (fe.hasMoreElements()) {
view.getDrawing().bringToFront(fe.nextFigure());
}
view.checkDamage();
}
}
protected class ExportViewAction extends EditorAction {
public static final int TRANSPARENT_BG_COLOR = 0x00efefef;
public ExportViewAction() {
this.putValue(Action.NAME, "exportView");
}
public boolean isEnabled() {
if (!super.isEnabled()) return false;
if (editorArea.getCurrentView() == null) return false;
return true;
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
DrawingView view = editorArea.getCurrentView();
JComponent comp = (JComponent) view;
File imageFile = (File) this.getValue(ConstantDefinition.FILE);
String imageType = (String) this.getValue(ConstantDefinition.IMAGE_TYPE);
Image image = null;
Graphics2D g2d = null;
//get image information
if (imageType.equalsIgnoreCase("gif")) {
Rectangle rect = GlobalFunction.checkMinimumViewport(view);
Dimension size = new Dimension(rect.width + 10, rect.height + 10);
image = comp.createImage(size.width, size.height);
g2d = (Graphics2D) image.getGraphics();
g2d.setColor(new Color(TRANSPARENT_BG_COLOR));
g2d.fillRect(0, 0, (int) size.getWidth(), (int) size.getHeight());
g2d.translate(-rect.x + 5, -rect.y + 5);
view.drawDrawing(g2d);
} else if (imageType.equalsIgnoreCase("svg")) {
// Get a DOMImplementation
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document
Document document = domImpl.createDocument(null, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
//ctx.setEmbeddedFontsOn(true);
// Create an instance of the SVG Generator
g2d = new SVGGraphics2D(ctx, false);
Rectangle rect = GlobalFunction.checkMinimumViewport(view);
Dimension size = new Dimension(rect.width * 2 + 10, rect.height * 2 + 10);
g2d.translate(-rect.x * 2 + 5, -rect.y * 2 + 5);
((SVGGraphics2D) g2d).setSVGCanvasSize(size);
((JComponent) view).print(g2d);
} else if (imageType.equalsIgnoreCase("emf")) {
}
//write image into file
try {
if (imageType.equalsIgnoreCase("gif")) {
FileOutputStream out = new FileOutputStream(imageFile);
FilteredImageSource fis =
new FilteredImageSource(image.getSource(),
new TransFilter(TRANSPARENT_BG_COLOR));
GifEncoder ge = new GifEncoder(fis, out);
ge.encode();
out.flush();
out.close();
} else if (imageType.equalsIgnoreCase("svg")) {
FileOutputStream out = new FileOutputStream(imageFile);
boolean useCSS = true; // we want to use CSS style attribute
//Writer outer = new OutputStreamWriter(out, "iso8859-1");
Writer outer = new OutputStreamWriter(out, "UTF-8");
((SVGGraphics2D) g2d).stream(outer, useCSS);
outer.flush();
outer.close();
} else {
//version 2
Rectangle rect = GlobalFunction.checkMinimumViewport(view);
Dimension size = new Dimension((rect.x + rect.width) * 2 + 5, (rect.y + rect.height) * 2 + 5);
Dimension oldSize = view.getSize();
((SaturnDrawingView) view).setSizeTemp(size);
/**
SaveAsDialog saveAs = new SaveAsDialog();
saveAs.addAllExportFileTypes();
saveAs.showExportDialog((Component) getValue(de.FeatureModellingTool.GraphicalEditor.ConstantDefinition.UI_FRAME),
"����ͼ���ʽ", (Component) view, view.getDrawing().getTitle());
**/
exportDialog.showExportDialog((Component) getValue(de.grapheditor.ConstantDefinition.UI_FRAME), "Export view as ...", (Component) view, view.getDrawing().getTitle());
((SaturnDrawingView) view).setSizeTemp(oldSize);
}
} catch (IOException ioe) {
System.out.println(ioe);
} finally {
if (g2d != null)
g2d.dispose();
this.putValue(ConstantDefinition.FILE, null);
this.putValue(ConstantDefinition.IMAGE_TYPE, null);
}
}
/**
* RGBImageFilter that turns on transparency for pixels of a specified color.
*/
class TransFilter extends RGBImageFilter {
int _transBG;
public TransFilter(int bg) {
_transBG = bg;
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb) {
// background color w/any alpha level? make it transparent
if ((rgb & 0x00ffffff) == _transBG) return _transBG;
return 0xff000000 | rgb; // make it 100% opaque
}
}
}
abstract class EditorAction extends AbstractAction {
public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
super.firePropertyChange(propertyName, oldValue, newValue);
}
}
abstract class AlignAction extends EditorAction {
protected transient Rectangle rect = null;
public boolean isEnabled() {
if (!super.isEnabled()) return false;
DrawingView currentView = editorArea.getCurrentView();
if (currentView == null) return false;
Vector selection = currentView.getSelection();
if (selection == null) return false;
if (selection.size() < 2) return false;
//AlignAction����connectionFigure��Ч�����Ҫ�жϵ�ǰѡ���ͼԪ�з�connectionFigure����Ŀ�Ƿ����2
Iterator iterator = selection.iterator();
int figureCount = 0;
while (iterator.hasNext()) {
Figure figure = (Figure) iterator.next();
if (!(figure instanceof ConnectionFigure))
figureCount++;
}
if (figureCount < 2) return false;
return true;
}
protected void caculateAffectedArea() {
Vector selection = editorArea.getCurrentView().getSelection();
Iterator iterator = selection.iterator();
rect = null;
while (iterator.hasNext()) {
Figure figure = (Figure) iterator.next();
if (!(figure instanceof ConnectionFigure)) {
if (rect == null) {
rect = figure.getDisplayBox();
} else {
rect.add(figure.getDisplayBox());
}
}
}
}
}
abstract class DistAction extends EditorAction {
protected transient Rectangle rect = null;
public boolean isEnabled() {
if (!super.isEnabled()) return false;
DrawingView currentView = editorArea.getCurrentView();
if (currentView == null) return false;
Vector selection = currentView.getSelection();
if (selection == null) return false;
if (selection.size() < 3) return false;
//DistAction����connectionFigure��Ч�����Ҫ�жϵ�ǰѡ���ͼԪ�з�connectionFigure����Ŀ�Ƿ����3
Iterator iterator = selection.iterator();
int figureCount = 0;
while (iterator.hasNext()) {
Figure figure = (Figure) iterator.next();
if (!(figure instanceof ConnectionFigure))
figureCount++;
}
if (figureCount < 3) return false;
return true;
}
protected void caculateAffectedArea() {
Vector selection = editorArea.getCurrentView().getSelection();
Iterator iterator = selection.iterator();
rect = null;
while (iterator.hasNext()) {
Figure figure = (Figure) iterator.next();
if (!(figure instanceof ConnectionFigure)) {
if (rect == null) {
rect = new Rectangle(figure.center().x, figure.center().y, 0, 0);
} else {
rect.add(figure.center());
}
}
}
}
}
protected class LeftAlignAction extends AlignAction {
public LeftAlignAction() {
this.putValue(Action.NAME, "��˶���");
this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/align_left.gif")));
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
caculateAffectedArea();
Vector selection = editorArea.getCurrentView().getSelection();
Iterator iterator = selection.iterator();
Rectangle r = new Rectangle();
int limit = rect.x;
while (iterator.hasNext()) {
Figure figure = (Figure) iterator.next();
if (!(figure instanceof ConnectionFigure)) {
figure.moveBy(limit - figure.getDisplayBox(r).x, 0);
}
}
editorArea.getCurrentView().repairDamage();
}
}
protected class VertAlignAction extends AlignAction {
public VertAlignAction() {
this.putValue(Action.NAME, "��ֱ����");
this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/align_vertical.gif")));
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
caculateAffectedArea();
Vector selection = editorArea.getCurrentView().getSelection();
Iterator iterator = selection.iterator();
Rectangle r = new Rectangle();
int limit = rect.x + rect.width / 2;
while (iterator.hasNext()) {
Figure figure = (Figure) iterator.next();
if (!(figure instanceof ConnectionFigure)) {
figure.moveBy(limit - figure.center().x, 0);
}
}
editorArea.getCurrentView().repairDamage();
}
}
protected class RightAlignAction extends AlignAction {
public RightAlignAction() {
this.putValue(Action.NAME, "�Ҷ˶���");
this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/align_right.gif")));
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
caculateAffectedArea();
Vector selection = editorArea.getCurrentView().getSelection();
Iterator iterator = selection.iterator();
Rectangle r = new Rectangle();
int limit = rect.x + rect.width;
while (iterator.hasNext()) {
Figure figure = (Figure) iterator.next();
if (!(figure instanceof ConnectionFigure)) {
figure.moveBy(limit - (figure.getDisplayBox(r).x + r.width), 0);
}
}
editorArea.getCurrentView().repairDamage();
}
}
protected class TopAlignAction extends AlignAction {
public TopAlignAction() {
this.putValue(Action.NAME, "���˶���");
this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/align_top.gif")));
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
caculateAffectedArea();
Vector selection = editorArea.getCurrentView().getSelection();
Iterator iterator = selection.iterator();
Rectangle r = new Rectangle();
int limit = rect.y;
while (iterator.hasNext()) {
Figure figure = (Figure) iterator.next();
if (!(figure instanceof ConnectionFigure)) {
figure.moveBy(0, limit - figure.getDisplayBox(r).y);
}
}
editorArea.getCurrentView().repairDamage();
}
}
protected class HoriAlignAction extends AlignAction {
public HoriAlignAction() {
this.putValue(Action.NAME, "ˮƽ����");
this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/align_horizontal.gif")));
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
caculateAffectedArea();
Vector selection = editorArea.getCurrentView().getSelection();
Iterator iterator = selection.iterator();
Rectangle r = new Rectangle();
int limit = rect.y + rect.height / 2;
while (iterator.hasNext()) {
Figure figure = (Figure) iterator.next();
if (!(figure instanceof ConnectionFigure)) {
figure.moveBy(0, limit - figure.center().y);
}
}
editorArea.getCurrentView().repairDamage();
}
}
protected class BotAlignAction extends AlignAction {
public BotAlignAction() {
this.putValue(Action.NAME, "�˶���");
this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/align_bottom.gif")));
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
caculateAffectedArea();
Vector selection = editorArea.getCurrentView().getSelection();
Iterator iterator = selection.iterator();
Rectangle r = new Rectangle();
int limit = rect.y + rect.height;
while (iterator.hasNext()) {
Figure figure = (Figure) iterator.next();
if (!(figure instanceof ConnectionFigure)) {
figure.moveBy(0, limit - (figure.getDisplayBox(r).y + r.height));
}
}
editorArea.getCurrentView().repairDamage();
}
}
protected class VertDistAction extends DistAction {
ArrayList arrayList = new ArrayList();
public VertDistAction() {
this.putValue(Action.NAME, "��ֱ�ֲ�");
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
caculateAffectedArea();
Iterator iterator = editorArea.getCurrentView().getSelection().iterator();
int figureCount = 0;
arrayList.clear();
while (iterator.hasNext()) {
Figure figure = (Figure) iterator.next();
if (!(figure instanceof ConnectionFigure)) {
figureCount++;
Point center = figure.center();
if (arrayList.isEmpty()) {
arrayList.add(figure);
} else {
int i;
for (i = 0; i < arrayList.size(); i++) {
Figure fig = (Figure) arrayList.get(i);
if (center.y < fig.center().y) {
arrayList.add(i, figure);
break;
}
}
if (i == arrayList.size())
arrayList.add(figure);
}
}
}
int filled = 0;
for (int i = 0; i < arrayList.size(); i++) {
Figure fig = (Figure) arrayList.get(i);
if ((i == 0) || (i == arrayList.size() - 1)) {
filled += fig.getDisplayBox().height / 2;
} else {
filled += fig.getDisplayBox().height;
}
}
double unit = (rect.height * 1.0 - filled) / (figureCount - 1);
Rectangle r = ((Figure) arrayList.get(0)).getDisplayBox();
int base = r.y + r.height;
for (int i = 1; i < arrayList.size() - 1; i++) {
Figure fig = (Figure) arrayList.get(i);
fig.moveBy(0, (int) (base + unit * i - fig.getDisplayBox(r).y));
base += r.height;
}
editorArea.getCurrentView().repairDamage();
}
}
protected class HoriDistAction extends DistAction {
ArrayList arrayList = new ArrayList();
public HoriDistAction() {
this.putValue(Action.NAME, "ˮƽ�ֲ�");
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
caculateAffectedArea();
Iterator iterator = editorArea.getCurrentView().getSelection().iterator();
int figureCount = 0;
arrayList.clear();
while (iterator.hasNext()) {
Figure figure = (Figure) iterator.next();
if (!(figure instanceof ConnectionFigure)) {
figureCount++;
Point center = figure.center();
if (arrayList.isEmpty()) {
arrayList.add(figure);
} else {
int i;
for (i = 0; i < arrayList.size(); i++) {
Figure fig = (Figure) arrayList.get(i);
if (center.x < fig.center().x) {
arrayList.add(i, figure);
break;
}
}
if (i == arrayList.size())
arrayList.add(figure);
}
}
}
int filled = 0;
for (int i = 0; i < arrayList.size(); i++) {
Figure fig = (Figure) arrayList.get(i);
if ((i == 0) || (i == arrayList.size() - 1)) {
filled += fig.getDisplayBox().width / 2;
} else {
filled += fig.getDisplayBox().width;
}
}
double unit = (rect.width * 1.0 - filled) / (figureCount - 1);
Rectangle r = ((Figure) arrayList.get(0)).getDisplayBox();
int base = r.x + r.width;
for (int i = 1; i < arrayList.size() - 1; i++) {
Figure fig = (Figure) arrayList.get(i);
fig.moveBy((int) (base + unit * i - fig.getDisplayBox(r).x), 0);
base += r.width;
}
editorArea.getCurrentView().repairDamage();
}
}
protected class ChangeColorAction extends EditorAction {
protected String attributeName;
public ChangeColorAction(String name, String attributeName) {
this.putValue(Action.NAME, name);
this.attributeName = attributeName;
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
Component uiFrame = (Component) this.getValue(ConstantDefinition.UI_FRAME);
FigureEnumeration fe = editorArea.getCurrentView().selectionElements();
Figure first = null;
if(fe.hasMoreElements())
first = fe.nextFigure();
else
return;
Color current = (Color) first.getAttribute(attributeName);
Color color = showDialog(uiFrame, "��ѡ����ɫ", current);
if (color == null) return;
first.setAttribute(attributeName, color);
while (fe.hasMoreElements()) {
fe.nextFigure().setAttribute(attributeName, color);
}
editorArea.getCurrentView().checkDamage();
}
}
protected final JColorChooser pane = new JColorChooser(Color.white);
protected final WindowAdapter windowListener = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Window w = e.getWindow();
w.setVisible(false);
}
};
protected final ComponentAdapter componentListener = new ComponentAdapter() {
public void componentHidden(ComponentEvent e) {
Window w = (Window) e.getComponent();
w.dispose();
}
};
protected Color showDialog(Component component, String title, Color color) throws HeadlessException {
//final JColorChooser pane = new JColorChooser(initialColor != null ?
// initialColor : Color.white);
AbstractColorChooserPanel[] accp = pane.getChooserPanels();
for(int i = 0; i < accp.length; i++){
pane.removeChooserPanel(accp[i]);
}
pane.addChooserPanel(rgbaChooser);
pane.addChooserPanel(hsbaChooser);
pane.addChooserPanel(dsChooser);
if(color != null)
pane.setColor(color);
ColorTracker ok = new ColorTracker(pane);
JDialog dialog = JColorChooser.createDialog(component, title, true, pane, ok, null);
dialog.addWindowListener(windowListener);
dialog.addComponentListener(componentListener);
dialog.setVisible(true); // blocks until user brings dialog down...
Color rst = ok.getColor();
dialog.removeWindowListener(windowListener);
dialog.removeComponentListener(componentListener);
pane.removeChooserPanel(rgbaChooser);
pane.removeChooserPanel(hsbaChooser);
pane.removeChooserPanel(dsChooser);
return rst;
}
class ColorTracker implements ActionListener, Serializable {
JColorChooser chooser;
Color color;
public ColorTracker(JColorChooser c) {
chooser = c;
}
public void actionPerformed(ActionEvent e) {
color = chooser.getColor();
}
public Color getColor() {
return color;
}
}
protected class InerDropTargetListener implements DropTargetListener {
public void dragEnter(DropTargetDragEvent dtde) {
}
public void dragOver(DropTargetDragEvent dtde) {
}
public void dropActionChanged(DropTargetDragEvent dtde) {
}
public void dragExit(DropTargetEvent dte) {
}
public void drop(DropTargetDropEvent dtde) {
if (!dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
dtde.rejectDrop();
return;
}
Object data = null;
try {
//DataFlavor[] flavors = dtde.getTransferable().getTransferDataFlavors();
//for(int i = 0; i < flavors.length;i ++ ){
// System.out.println(flavors[i] + " " + i);
//}
data = dtde.getTransferable().getTransferData(DataFlavor.stringFlavor);
DataFlavor.getTextPlainUnicodeFlavor();
} catch (Throwable e) {
dtde.rejectDrop();
System.out.println(e);
return;
}
String id = null;
if (String.class.isInstance(data)) {
id = (String) data;
} else if (InputStream.class.isInstance(data)) {
String charset = DataFlavor.stringFlavor.getParameter("charset").trim();
InputStream input = (InputStream) data;
InputStreamReader isr = null;
StringBuffer str = new StringBuffer();
int in = -1;
try {
while ((in = isr.read()) >= 0) {
if (in != 0)
str.append((char) in);
}
id = str.toString();
} catch (IOException ioe) {
dtde.dropComplete(false);
return;
}
}
dtde.dropComplete(false);
return;
}
};
}