package de.FeatureModellingTool.Customize.ui;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Iterator;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.tree.TreePath;
import org.jdesktop.swingx.JXTreeTable;
import de.FeatureModellingTool.Customize.ConstantDefinition;
import de.FeatureModellingTool.Customize.Customization;
import de.FeatureModellingTool.Customize.CustomizationVersion;
import de.FeatureModellingTool.FeatureModel.ConstraintModel;
import de.FeatureModellingTool.FeatureModel.FeatureModel;
import de.FeatureModellingTool.FeatureModel.Variability;
import de.reuse.Context;
import de.reuse.ContextImplementation;
public class ConstraintExplorerImplementation extends JScrollPane implements ConstraintExplorer {
private static String CE_SELECTED_NODE = "CE_SELECTED_NODE";
private static String CE_SELECTED_CON = "CE_SELECTED_CON";
private static final long serialVersionUID = 0L;
public ConstraintExplorerImplementation(FeatureModel featureModel , ConstraintModel constraintModel) {
this.featureModel = featureModel;
this.constraintModel = constraintModel;
ConstraintExplorerTreeNode root = new ConstraintExplorerTreeNode();
this.constraintExplorerTreeModel = new ConstraintExplorerTreeModel(root);
this.constraintExplorerTreeModel.getContext().putValue(ConstantDefinition.FEATURE_MODEL , this.featureModel);
this.constraintExplorerTreeModel.getContext().putValue(ConstantDefinition.CONSTRAINT_MODEL , this.constraintModel);
this.treeTable = new JXTreeTable(this.constraintExplorerTreeModel);
// this.treeTable.setHighlighters(new AlternateRowHighlighter(Color.WHITE , new Color(240 , 255 , 255) , Color.BLACK));
this.treeTable.setDefaultRenderer(Object.class, this.constraintExplorerTreeModel.getCellRenderer());
this.treeTable.getTableHeader().setPreferredSize(new Dimension(treeTable.getTableHeader().getPreferredSize().width, 32));
this.treeTable.getTableHeader().setDefaultRenderer(this.constraintExplorerTreeModel.getHeaderRender());
this.treeTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
this.treeTable.setShowHorizontalLines(true);
this.treeTable.setShowVerticalLines(true);
this.treeTable.setRowHeight(24);
this.treeTable.getColumn(0).setPreferredWidth(200);
this.treeTable.getColumn(0).setMinWidth(200);
for(int i=1 ; i<this.treeTable.getColumnCount() ; i++) {
this.treeTable.getColumn(i).setPreferredWidth(65);
this.treeTable.getColumn(i).setMaxWidth(65);
this.treeTable.getColumn(i).setMinWidth(65);
}
this.treeTable.updateUI();
this.context.addContextChangeListener(new ContextChangeListener());
this.treeTable.addMouseListener(new ConstraintExplorerProbe());
this.popupMenu = new JPopupMenu();
this.gatherColumnAction = new GatherColumnAction();
this.popupMenu.add(this.gatherColumnAction);
this.gatherFeatureAction = new GatherFeatureAction();
this.popupMenu.add(this.gatherFeatureAction);
this.selectAction = new SelectAction();
this.popupMenu.add(this.selectAction);
this.unselectAction = new UnselectAction();
this.popupMenu.add(this.unselectAction);
this.undecidedAction = new UndecidedAction();
this.popupMenu.add(this.undecidedAction);
}
protected final ContextImplementation context = new ContextImplementation();
public Context getContext() {
return this.context;
}
FeatureModel featureModel = null;
ConstraintModel constraintModel = null;
CustomizationVersion customizationVersion = null;
protected class ContextChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
constraintExplorerTreeModel.getContext().putValue(propertyName , e.getNewValue());
if (ConstantDefinition.CUSTOMIZATION_VERSION.equals(propertyName)) {
customizationVersion = (CustomizationVersion) e.getNewValue();
}
treeTable.updateUI();
treeTable.getTableHeader().updateUI();
}
}
private ConstraintExplorerTreeModel constraintExplorerTreeModel = null;
private JXTreeTable treeTable = null;
public JComponent getExplorerComponent() {
return this.treeTable;
}
// ������Ϣ
protected JPopupMenu popupMenu = null;
protected GatherColumnAction gatherColumnAction = null;
protected class GatherColumnAction extends AbstractAction {
protected static final long serialVersionUID = 0L;
public GatherColumnAction() {
this.putValue(Action.NAME, "�ۼ����Լ��");
}
public boolean isEnabled() {
return getContext().getValue(CE_SELECTED_NODE)!=null;
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
ConstraintExplorerTreeNode node = (ConstraintExplorerTreeNode)getContext().getValue(CE_SELECTED_NODE);
if (node!=null && node.getFeature()!=null) {
if (constraintExplorerTreeModel.moveColumnByFeature(node.getFeature())) {
treeTable.updateUI();
treeTable.getTableHeader().updateUI();
}
}
}
public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
super.firePropertyChange(propertyName, oldValue, newValue);
}
}
protected SelectAction selectAction = null;
protected UnselectAction unselectAction = null;
protected UndecidedAction undecidedAction = null;
protected class SelectAction extends AbstractAction {
protected static final long serialVersionUID = 0L;
public SelectAction() {
this.putValue(Action.NAME, "����_ѡ��");
}
public boolean isEnabled() {
ConstraintExplorerTreeNode node= (ConstraintExplorerTreeNode)getContext().getValue(CE_SELECTED_NODE);
return node!=null && node.getFeature()!=null && node.getFeature().getVariability().equals(Variability.Optional)
&& !customizationVersion.isFeatureCustomizedByParentVersion(node.getFeature().getID());
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
ConstraintExplorerTreeNode node = (ConstraintExplorerTreeNode)getContext().getValue(CE_SELECTED_NODE);
if (node!=null && node.getFeature()!=null) {
customizationVersion.setCustomization(node.getFeature().getID() , Customization.Selected);
treeTable.updateUI();
treeTable.getTableHeader().updateUI();
}
}
public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
super.firePropertyChange(propertyName, oldValue, newValue);
}
}
protected class UnselectAction extends AbstractAction {
protected static final long serialVersionUID = 0L;
public UnselectAction() {
this.putValue(Action.NAME, "����_ȥ��");
}
public boolean isEnabled() {
ConstraintExplorerTreeNode node= (ConstraintExplorerTreeNode)getContext().getValue(CE_SELECTED_NODE);
return node!=null && node.getFeature()!=null && node.getFeature().getVariability().equals(Variability.Optional)
&& !customizationVersion.isFeatureCustomizedByParentVersion(node.getFeature().getID());
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
ConstraintExplorerTreeNode node = (ConstraintExplorerTreeNode)getContext().getValue(CE_SELECTED_NODE);
if (node!=null && node.getFeature()!=null) {
customizationVersion.setCustomization(node.getFeature().getID() , Customization.Unselected);
treeTable.updateUI();
treeTable.getTableHeader().updateUI();
}
}
public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
super.firePropertyChange(propertyName, oldValue, newValue);
}
}
protected class UndecidedAction extends AbstractAction {
protected static final long serialVersionUID = 0L;
public UndecidedAction() {
this.putValue(Action.NAME, "����_��ԭ");
}
public boolean isEnabled() {
ConstraintExplorerTreeNode node= (ConstraintExplorerTreeNode)getContext().getValue(CE_SELECTED_NODE);
return node!=null && node.getFeature()!=null && node.getFeature().getVariability().equals(Variability.Optional)
&& !customizationVersion.isFeatureCustomizedByParentVersion(node.getFeature().getID());
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
ConstraintExplorerTreeNode node = (ConstraintExplorerTreeNode)getContext().getValue(CE_SELECTED_NODE);
if (node!=null && node.getFeature()!=null) {
customizationVersion.setCustomization(node.getFeature().getID() , Customization.Undecided);
treeTable.updateUI();
treeTable.getTableHeader().updateUI();
}
}
public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
super.firePropertyChange(propertyName, oldValue, newValue);
}
}
protected GatherFeatureAction gatherFeatureAction = null;
protected class GatherFeatureAction extends AbstractAction {
protected static final long serialVersionUID = 0L;
public GatherFeatureAction() {
this.putValue(Action.NAME, "�ۼ��������");
}
public boolean isEnabled() {
return getContext().getValue(CE_SELECTED_CON)!=null;
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
Integer columnIndex = (Integer)getContext().getValue(CE_SELECTED_CON);
List<TreePath> pathes = constraintExplorerTreeModel.getConstraintPathes(columnIndex);
treeTable.collapseAll();
for (Iterator<TreePath> itPath = pathes.iterator() ; itPath.hasNext() ; ) {
TreePath path = itPath.next();
treeTable.expandPath(path);
}
}
public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
super.firePropertyChange(propertyName, oldValue, newValue);
}
}
protected class ConstraintExplorerProbe extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
int columnIndex = treeTable.columnAtPoint(e.getPoint());
if (columnIndex==0) {
TreePath tp = treeTable.getPathForLocation(e.getX(), e.getY());
if (tp != null) {
Object node = tp.getLastPathComponent();
// if ((node != null) && node instanceof ConstraintExplorerTreeNode) {
getContext().putValue(CE_SELECTED_NODE , node);
// }
}
}
if (columnIndex>0) {
getContext().putValue(CE_SELECTED_CON , new Integer(columnIndex));
}
popupMenu.show(treeTable , e.getX() , e.getY());
gatherColumnAction.firePropertyChange("enabled" , !gatherColumnAction.isEnabled() , gatherColumnAction.isEnabled());
selectAction.firePropertyChange("enabled" , !selectAction.isEnabled() , selectAction.isEnabled());
unselectAction.firePropertyChange("enabled" , !unselectAction.isEnabled() , unselectAction.isEnabled());
undecidedAction.firePropertyChange("enabled" , !undecidedAction.isEnabled() , undecidedAction.isEnabled());
gatherFeatureAction.firePropertyChange("enabled" , !gatherFeatureAction.isEnabled() , gatherFeatureAction.isEnabled());
}
}
}
}