package de.FeatureModellingTool.ElementInfoEditor;
import de.reuse.Context;
import de.reuse.ContextImplementation;
import de.reuse.DrawingEditorTabbedPane;
import de.reuse.GroupMap;
import de.FeatureModellingTool.FeatureModel.FeatureModel;
import de.FeatureModellingTool.GraphicalEditor.GraphicalEditor;
import javax.swing.*;
import applet.WebConstantDefinition;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import manager.ManagerInterface;
import component.FComponent;
import research.*;
/**
* Created by IntelliJ IDEA.
* User: saturn
* Date: 2003-10-14
* Time: 15:11:02
* To change this template use Options | File Templates.
*/
public class FeatureDescriptionEditorImplementation implements FeatureDescriptionEditor, ConstantDefinition {
/** context attribures quick reference **/
protected FeatureModel featureModel = null;
protected Component container = null;
protected GraphicalEditor graphicalEditor = null;
protected GroupMap informationMap = null;
/** **/
protected final ContextImplementation context = new ContextImplementation();
protected final EntryAction entryAction = new EntryAction();
protected boolean isEnable = false;
protected final FigureSelectionChangeListener figureSelectionChangeListener = new FigureSelectionChangeListener();
protected final CurrentViewChangeListener currentViewChangeListener = new CurrentViewChangeListener();
public Action getEntryAction() {
return entryAction;
}
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 (FEATURE_MODEL.equals(propertyName)) {
featureModel = (FeatureModel) e.getNewValue();
} else if (CONTAINER.equals(propertyName)) {
container = (Component) e.getNewValue();
} else if (GRAPHICAL_EDITOR.equals(propertyName)) {
if (graphicalEditor != null) {
graphicalEditor.removePropertyChangeListener(DrawingEditor.SELECTION_CHANGED, figureSelectionChangeListener);
graphicalEditor.removePropertyChangeListener(DrawingEditor.CURRENT_VIEW_CHANGED, currentViewChangeListener);
}
graphicalEditor = (GraphicalEditor) e.getNewValue();
if (graphicalEditor != null) {
graphicalEditor.addPropertyChangeListener(DrawingEditor.SELECTION_CHANGED, figureSelectionChangeListener);
graphicalEditor.addPropertyChangeListener(DrawingEditor.CURRENT_VIEW_CHANGED, currentViewChangeListener);
}
} else if (INFOMATION_MAP.equals(propertyName)) {
informationMap = (GroupMap) e.getNewValue();
}
}
protected class EntryAction extends AbstractAction {
public EntryAction() {
this.putValue(Action.NAME, "��������");
}
public boolean isEnabled() {
if (!super.isEnabled()) return false;
if (container == null) return false;
if (!isEnable) return false;
return true;
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
ArrayList selection = graphicalEditor.getSelection();
String featureID = null;
for (int i = 0; i < selection.size(); i++) {
String aID = (String) selection.get(i);
if (featureModel.containsFeature(aID)) {
featureID = aID;
break;
}
}
FDE fde = new FDE(informationMap, featureID);
JDialog dialog;
dialog = new JDialog((Frame) container, "���������༭", true);
dialog.getContentPane().setLayout(new BorderLayout());
dialog.getContentPane().add(fde, BorderLayout.CENTER);
/**
if (JDialog.isDefaultLookAndFeelDecorated()) {
boolean supportsWindowDecorations =
UIManager.getLookAndFeel().getSupportsWindowDecorations();
if (supportsWindowDecorations) {
dialog.setUndecorated(true);
dialog.getRootPane().setWindowDecorationStyle(JRootPane.INFORMATION_DIALOG);
}
}
**/
dialog.pack();
dialog.setLocationRelativeTo(container);
dialog.setVisible(true);
dialog.dispose();
}
public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
super.firePropertyChange(propertyName, oldValue, newValue);
}
}
protected class FigureSelectionChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if (DrawingEditor.SELECTION_CHANGED.equals(propertyName)) {
updateUIState();
}
}
}
protected class CurrentViewChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if (DrawingEditor.CURRENT_VIEW_CHANGED.equals(propertyName)) {
updateUIState();
}
}
}
protected void updateUIState() {
ArrayList selection = graphicalEditor.getSelection();
int size = 0;
if (selection == null) {
size = 0;
} else {
size = 0;
for (int i = 0; i < selection.size(); i++) {
String aID = (String) selection.get(i);
if (featureModel.containsFeature(aID)) {
size++;
}
}
}
boolean oldV = entryAction.isEnabled();
isEnable = (size == 1 ? true : false);
boolean newV = entryAction.isEnabled();
if (oldV != newV) {
entryAction.firePropertyChange("enabled", Boolean.valueOf(oldV), Boolean.valueOf(newV));
}
}
public FeatureDescriptionEditorImplementation() {
context.addContextChangeListener(new ContextChangeListener());
}
}
class FDE extends JComponent {
protected JPanel capricePanel = null;
protected DrawingEditorTabbedPane drawingEditor = null;
protected SaturnToolBar toolBar = null;
protected JToolBar frameToolBar = null;
protected JPopupMenu popupMenu = null;
protected GroupMap informationMap = null;
protected String featureID = null;
protected FComponent alignAndDistService = null;
public FDE(GroupMap informationMap, String featureID) {
this.setLayout(new BorderLayout());
this.setOpaque(true);
this.informationMap = informationMap;
this.featureID = featureID;
capricePanel = new JPanel();
drawingEditor = new DrawingEditorTabbedPane();
toolBar = new SaturnToolBar();
frameToolBar = new JToolBar();
capricePanel.setMinimumSize(new Dimension(600, 400));
capricePanel.setPreferredSize(capricePanel.getMinimumSize());
//drawingEditor.setMinimumSize(new Dimension(400, 300));
//drawingEditor.setPreferredSize(drawingEditor.getMinimumSize());
toolBar.setFloatable(false);
toolBar.setOrientation(JToolBar.VERTICAL);
toolBar.getController().setDrawingEditor(drawingEditor);
JPanel drawingArea = new JPanel();
drawingArea.setLayout(new BorderLayout());
drawingArea.add(drawingEditor, BorderLayout.CENTER);
drawingArea.add(toolBar, BorderLayout.WEST);
capricePanel.add(drawingArea, SwingConstants.CENTER);
NewViewAction newViewAction;
newViewAction = new NewViewAction();
// newViewAction.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/humor.gif")));
try{
newViewAction.putValue(Action.SMALL_ICON, new ImageIcon(new URL(WebConstantDefinition.RES_HOST+"humor.gif")));
}catch(Exception e){
e.printStackTrace();
}
newViewAction.putValue("drawingEditor", drawingEditor);
frameToolBar.add(newViewAction);
frameToolBar.setFloatable(false);
frameToolBar.setOrientation(JToolBar.HORIZONTAL);
frameToolBar.setBorder(BorderFactory.createEmptyBorder());
capricePanel.add(frameToolBar, SwingConstants.NORTH);
this.add(capricePanel, BorderLayout.CENTER);
popupMenu = new JPopupMenu();
drawingEditor.addPropertyChangeListener(DrawingEditor.POPUP_MENU_TRIGGERED,
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if (DrawingEditor.POPUP_MENU_TRIGGERED.equals(propertyName)) {
int childCount = popupMenu.getComponentCount();
if (childCount > 0) {
Point p = (Point) e.getNewValue();
popupMenu.show((Component) e.getSource(), p.x, p.y);
}
}
}
});
ManagerInterface mi = research.align.Manager.getInstance();
try {
alignAndDistService = (FComponent) mi.getInstance(research.align.ConstantDefinition.ALIGN_AND_DISTRIBUTION_SERVICE_NAME, FComponent.class);
} catch (Exception e) {
e.printStackTrace();
}
JMenu alignAndDist = new JMenu("������ֲ�");
alignAndDist.add((Action) alignAndDistService.getInterface(research.align.ConstantDefinition.LEFT_ALIGN_INTERFACE));
alignAndDist.add((Action) alignAndDistService.getInterface(research.align.ConstantDefinition.VERTICAL_ALIGN_INTERFACE));
alignAndDist.add((Action) alignAndDistService.getInterface(research.align.ConstantDefinition.RIGHT_ALIGN_INTERFACE));
alignAndDist.addSeparator();
alignAndDist.add((Action) alignAndDistService.getInterface(research.align.ConstantDefinition.TOP_ALIGN_INTERFACE));
alignAndDist.add((Action) alignAndDistService.getInterface(research.align.ConstantDefinition.HORIZONTAL_ALIGN_INTERFACE));
alignAndDist.add((Action) alignAndDistService.getInterface(research.align.ConstantDefinition.BOTTOM_ALIGN_INTERFACE));
alignAndDist.addSeparator();
alignAndDist.add((Action) alignAndDistService.getInterface(research.align.ConstantDefinition.VERTICAL_DISTRIBUTION_INTERFACE));
alignAndDist.add((Action) alignAndDistService.getInterface(research.align.ConstantDefinition.HORIZONTAL_DISTRIBUTION_INTERFACE));
popupMenu.add(alignAndDist);
context.Context cont = (context.Context) alignAndDistService.getInterface(research.align.ConstantDefinition.CONTEXT_INTERFACE);
cont.putValue(research.align.ConstantDefinition.DRAWING_EDITOR, drawingEditor);
Set set = informationMap.get(featureID);
if (set != null) {
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Drawing drawing = (Drawing) iterator.next();
SaturnDrawingView view = createDrawingView();
view.setDrawing(drawing);
drawingEditor.add((DrawingView) view);
}
}
}
protected class NewViewAction extends AbstractAction {
protected boolean isDoing = false;
protected int count = 0;
public NewViewAction() {
super("New View");
isDoing = false;
}
public void actionPerformed(ActionEvent e) {
if (isDoing)
return;
isDoing = true;
ViewInfoPane viewInfoPane = new ViewInfoPane();
for (; ;) {
int result = JOptionPane.showConfirmDialog(FDE.this, viewInfoPane, "�½���ͼ", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
if (viewInfoPane.isInfoValid() == false) {
JOptionPane.showMessageDialog(FDE.this, viewInfoPane.getErrorInfo(), "���洰��", JOptionPane.ERROR_MESSAGE);
continue;
} else {
boolean flag = false;
Set set = informationMap.get(featureID);
if (set != null) {
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Drawing drawing = (Drawing) iterator.next();
if (drawing.getTitle().equals(viewInfoPane.getName())) {
flag = true;
break;
}
}
}
if (flag) {
JOptionPane.showMessageDialog(FDE.this, "��ͬ���ֵ���ͼ�Ѿ����ڣ�", "���洰��", JOptionPane.ERROR_MESSAGE);
continue;
}
}
} else {
isDoing = false;
return;
}
break;
}
SaturnDrawingView view = createDrawingView();
SaturnDrawing drawing = createDrawing();
drawing.setTitle(viewInfoPane.getName());
view.setDrawing(drawing);
DrawingEditor drawingEditor = (DrawingEditor) getValue("drawingEditor");
drawingEditor.add(view);
informationMap.add(featureID, drawing);
isDoing = false;
}
}
protected SaturnDrawingView createDrawingView() {
return new SaturnDrawingView(800, 2400);
}
protected SaturnDrawing createDrawing() {
return new SaturnDrawing();
}
}
class ViewInfoPane extends JPanel {
protected JLabel nameLabel = new JLabel("��ͼ����");
protected JTextField nameField = new JTextField(25);
protected String errorInfo = null;
public ViewInfoPane() {
super();
init();
}
protected void init() {
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gbl);
//row1
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridwidth = 1;
add(Box.createHorizontalStrut(10), gbc);
add(nameLabel, gbc);
add(Box.createHorizontalStrut(15), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
add(nameField, gbc);
gbc.weightx = 0;
add(Box.createVerticalStrut(10), gbc);
}
public String getName() {
return nameField.getText();
}
public void clear() {
nameField.setText("");
}
public boolean isInfoValid() {
if ((nameField.getText() == null) || (nameField.getText().trim().equals(""))) {
errorInfo = "��������ͼ����.";
return false;
}
nameField.setText(nameField.getText().trim());
return true;
}
public String getErrorInfo() {
return errorInfo;
}
}