package de.FeatureModellingTool.DictEditor;
import de.reuse.Context;
import de.reuse.ContextImplementation;
import de.FeatureModellingTool.FeatureModel.FeatureModel;
import de.FeatureModellingTool.FeatureModel.Feature;
import de.FeatureModellingTool.FeatureModel.FeatureModelProperties;
import de.FeatureModellingTool.DictEditor.TermiPanelEditor.TermiControlPanel;
import de.FeatureModellingTool.DictEditor.TermiPanelEditor.TermiInfoPanel;
import de.FeatureModellingTool.DictEditor.TermiPanelEditor.TermiDictGUIMediator;
import javax.swing.*;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
import javax.swing.text.html.HTMLDocument;
import javax.swing.border.Border;
import javax.swing.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.util.TreeSet;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Locale;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import java.io.File;
import java.io.FileWriter;
import java.text.Collator;
/**
* Recoder: Raymond
* Date: 2003-10-7
* Time: 14:56:35
*/
public class DictEditorImplementation extends JFrame implements DictEditor, DictionaryModel
{
protected final ContextImplementation context = new ContextImplementation();
protected final EntryAction entryAction = new EntryAction();
protected FeatureModel featureModel = null;
protected FeatureModelPropertyChangeListener featureModelPropertyChangeListener = new FeatureModelPropertyChangeListener();
protected static String htmlModel = ConstantDefinition.RESOURCEA_PATH + "featureModel.html";
protected HtmlGeneratorImpl htmlG = null;
protected TermiDictGUIMediator mediator = null;
//pane
JSplitPane splitPane = null;
JTabbedPane leftPane = null;
JPanel rightPane = null;
JViewport vp = null;
JScrollPane scroller = null;
JEditorPane htmlPane = null;
TermiInfoPanel termiInfoPane = new TermiInfoPanel();
JPanel featurePane = null;
TermiControlPanel terminologyPane = null;
JPanel labelPane = null;
JPanel listPane = null;
//findPane
boolean findPaneIsShow = false;
JPanel findPane = null;
JLabel findLabel = new JLabel("result");
//findList
protected JList findList = new JList();
protected DefaultListModel findListModel = new DefaultListModel();
//list0
protected TreeSet featureTree = new TreeSet
(
new Comparator()
{
public int compare(Object a, Object b)
{
Feature featureA = (Feature) a;
Feature featureB = (Feature) b;
//2.15
Collator c = Collator.getInstance( Locale.CHINA );
if (featureA.getName().equals(featureB.getName()))
return 1;
else
return c.compare(featureA.getName(),featureB.getName());
/* if (featureA.getName().equals(featureB.getName()))
{
return 1;
}
else
return (featureA.getName()).compareTo(featureB.getName());
*/
}
}
);
protected DefaultListModel featureListModel = new DefaultListModel();
protected JList featureList = new JList();
//other:labels
JLabel keyLabel = new JLabel(" Type the feature name:");
JLabel listLabel = new JLabel(" Choose match:");
JTextField keyField = new JTextField();
JOnValueChanged textChangeListener = new JOnValueChanged();
protected ListListener listListener = new ListListener();
protected FListListener findlistListener = new FListListener();
public Context getContext()
{
return context;
}
public Action getEntryAction()
{
return entryAction;
}
protected class EntryAction extends AbstractAction
{
public EntryAction()
{
this.putValue(Action.NAME, "����ʵ�");
}
public boolean isEnabled()
{
if (!super.isEnabled()) return false;
if (featureModel == null) return false;
return true;
}
public void actionPerformed(ActionEvent e)
{
if (!isEnabled()) return;
DictEditorImplementation.this.setVisible(true);
}
public void firePropertyChange(String propertyName, Object oldValue, Object newValue)
{
super.firePropertyChange(propertyName, oldValue, newValue);
}
}
protected class ContextChangeListener implements PropertyChangeListener
{
public void propertyChange(PropertyChangeEvent e)
{
contextChange(e);
}
}
protected void contextChange(PropertyChangeEvent e)
{
String propertyName = e.getPropertyName();
if (ConstantDefinition.FEATURE_MODEL.equals(propertyName))
{
if (featureModel != null)
{
featureModel.removePropertyChangeListener(featureModelPropertyChangeListener);
}
boolean oldV = entryAction.isEnabled();
featureModel = (FeatureModel) e.getNewValue();
if (featureModel != null)
{
featureModel.addPropertyChangeListener(featureModelPropertyChangeListener);
setFeatureModel(featureModel);
}
boolean newV = entryAction.isEnabled();
if (oldV != newV)
{
entryAction.firePropertyChange("enabled", Boolean.valueOf(oldV), Boolean.valueOf(newV));
}
}
}
protected class FeatureModelPropertyChangeListener implements PropertyChangeListener
{
public void propertyChange(PropertyChangeEvent e)
{
String propertyName = e.getPropertyName();
if (FeatureModelProperties.FEATURE_ADDED.equals(propertyName))
{
Feature feature = (Feature) e.getNewValue();
featureAdded(feature);
}
else if (FeatureModelProperties.FEATURE_REMOVED.equals(propertyName))
{
Feature feature = (Feature) e.getOldValue();
featureRemoved(feature);
}
else if (e.getSource() instanceof Feature)
{
Feature feature = (Feature) e.getSource();
featureUpdated(feature);
}
}
}
//
public DictEditorImplementation()
{
context.addContextChangeListener(new ContextChangeListener());
setGlassPane(new GlassPane());
splitPane = new JSplitPane();
createLeftPane();
createRightPane();
splitPane.setDividerSize(2);
splitPane.setLeftComponent(leftPane);
splitPane.setRightComponent(rightPane);
getContentPane().add(splitPane);
splitPane.setDividerLocation(290);
setSize(new Dimension(650, 600));
setLocation(220, 120);
htmlG = new HtmlGeneratorImpl(htmlModel);
// first display the html page.
initHtmlPane();
mediator = new TermiDictGUIMediator(this);
}
//init list
public void setFeatureTree(FeatureModel featureModel)
{
Iterator iterator;
iterator = featureModel.getAllFeature().keySet().iterator();
while (iterator.hasNext())
{
String id = (String) iterator.next();
Feature feature = (Feature) featureModel.getAllFeature().get(id);
featureTree.add(feature);
}
}
public void setFeatureListModel(TreeSet featureTree)
{
Iterator iterator;
featureListModel.clear();
iterator = featureTree.iterator();
while (iterator.hasNext())
{
Feature feature = (Feature) iterator.next();
featureListModel.addElement(feature);
}
}
public void setFeatureList(DefaultListModel featureListModel)
{
featureList.setModel(featureListModel);
featureList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
featureList.setVisibleRowCount(23);
featureList.addListSelectionListener(listListener);
}
public void setFindList(DefaultListModel featureListModel)
{
findList.setModel(findListModel);
findList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
findList.setVisibleRowCount(5);
findList.addListSelectionListener(findlistListener);
}
// methods about features
public void setFeatureModel(FeatureModel featureModel)
{
System.out.println("---setFeatureModel");
this.featureModel = featureModel;
setFeatureTree(featureModel);
setFeatureListModel(featureTree);
setFeatureList(featureListModel);
//html Generator
htmlG.setFeatureModel(featureModel);
}
public void featureAdded(Feature feature)
{
System.out.println("----featureAdded");
featureTree.add(feature);
setFeatureListModel(featureTree);
}
public void featureRemoved(Feature feature)
{
System.out.println("----featureRemoved");
int num = featureList.getSelectedIndex();
Feature featureDel = null;
boolean flagsame = false;
if (num > -1)
{
featureDel = (Feature) featureListModel.getElementAt(num);
if (feature.equals(featureDel))
{
flagsame = true;
}
}
// update list
featureTree.remove(feature);
featureListModel.removeElement(feature);
//update content
if (flagsame)
{
//featureDisp(feature);
indexDisp();
keyField.setText("");
}
}
public void featureUpdated(Feature feature)
{
//System.out.println("----featureUpdated");
featurePane.updateUI();
}
///init pane
protected void createLeftPane()
{
// featurePane
createFeturePane();
// terminologyPane
terminologyPane = new TermiControlPanel();
// leftPane
leftPane = new JTabbedPane();
leftPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
leftPane.addTab("feature", featurePane);
leftPane.addTab("terminology", terminologyPane);
leftPane.setSelectedComponent(featurePane);
/* tabbedPane test
*/
leftPane.addChangeListener
(
new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
if (leftPane.getSelectedIndex() == 1)
{
vp.remove(htmlPane);
vp.add(termiInfoPane);
//termiInfoPane.updateUI();
}
else
{
vp.remove(termiInfoPane);
vp.add(htmlPane);
}
}
}
);
}
protected void createFeturePane()
{
// labelPane
JPanel labelPane = new JPanel();
//BorderLayout
labelPane.setLayout(new BorderLayout(0, 3));
labelPane.add(keyLabel, BorderLayout.NORTH);
labelPane.add(keyField, BorderLayout.CENTER);
labelPane.add(listLabel, BorderLayout.SOUTH);
/*
// GridLayout
labelPane.setLayout( new GridLayout(3, 1, 0, 4));
labelPane.add(keyLabel);
labelPane.add(keyField);
labelPane.add(listLabel);
*/
keyField.getDocument().addDocumentListener(textChangeListener);
keyField.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int fnum;
fnum = findSearchList(keyField.getText());
if (fnum > 0)
{
if (!findPaneIsShow)
{
createFindPane();
}
findPane.setVisible(true);
if (fnum == 1)
{
findLabel.setText("1 Topic Found");
}
else
{
findLabel.setText(fnum + " Topics Found");
System.out.println("Topics Found :--" + findListModel.size());
}
showFindFeature();
}
else
{
if (findPaneIsShow)
{
findPane.setVisible(false);
}
IndexSearchFeature();
}
}
});
//listPane
listPane = new JPanel();
listPane.setLayout(new BoxLayout(listPane, BoxLayout.Y_AXIS));
listPane.add(Box.createRigidArea(new Dimension(10, 1)));
listPane.add(new JScrollPane(featureList));
//featurePane
featurePane = new JPanel(new BorderLayout());
featurePane.add(listPane, BorderLayout.CENTER);
featurePane.add(labelPane, BorderLayout.NORTH);
}
protected void createRightPane()
{
rightPane = new JPanel(new BorderLayout());
//htmlPane = new JEditorPane();
scroller = new JScrollPane();//scroller = new JScrollPane(htmlPane);
vp = scroller.getViewport();
rightPane.add(scroller, BorderLayout.CENTER);
//share border
Border border = BorderFactory.createEmptyBorder();
scroller.setBorder(border);
rightPane.setBorder(border);
splitPane.setBorder(border);
}
protected void initHtmlPane()
{
htmlPane = new JEditorPane();
htmlPane.setEditable(false);
htmlPane.addHyperlinkListener(createHyperLinkListener());
vp.add(htmlPane);
// disp index
indexDisp();
}
protected void createFindPane()
{
//findPane
findPane = new JPanel();
setFindList(findListModel);
findPane.setLayout(new BoxLayout(findPane, BoxLayout.Y_AXIS));
findPane.add(Box.createRigidArea(new Dimension(10, 1)));
findPane.add(findLabel, BorderLayout.NORTH);
findPane.add(new JScrollPane(findList));
featurePane.add(findPane, BorderLayout.SOUTH);
featurePane.updateUI();
// set the flag
findPaneIsShow = true;
}
///
protected HyperlinkListener createHyperLinkListener()
{
return new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent e)
{
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
{
if (e instanceof HTMLFrameHyperlinkEvent)
{
((HTMLDocument) htmlPane.getDocument()).processHTMLFrameHyperlinkEvent(
(HTMLFrameHyperlinkEvent) e);
}
else
{
hyperClick((Feature) featureModel.getAllFeature().get(e.getDescription()));
}
}
if (e.getEventType() == HyperlinkEvent.EventType.ENTERED)
{
// show tip
}
}
};
}
//responese methods
public void setKeyField(String title)
{
keyField.getDocument().removeDocumentListener(textChangeListener);
keyField.setText(title);
keyField.getDocument().addDocumentListener(textChangeListener);
}
public void keyFieldCharReply()
{
if (findPaneIsShow)
{
findPane.setVisible(false);
}
//set selected index;
int listPosition = 0;
listPosition = indexSearchList(keyField.getText());
featureList.removeListSelectionListener(listListener);
featureList.setSelectedIndex(listPosition);
featureList.addListSelectionListener(listListener);
}
class JOnValueChanged implements DocumentListener
{
public void changedUpdate(DocumentEvent e)
{
//System.out.println("Attribute Changed"+e);
}
public void insertUpdate(DocumentEvent e)
{
keyFieldCharReply();
}
public void removeUpdate(DocumentEvent e)
{
keyFieldCharReply();
}
}
class ListListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
if (!e.getValueIsAdjusting())
{
featureListSelected();
}
}
}
class FListListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
if (!e.getValueIsAdjusting())
{
findListSelected();
}
}
}
public void listSelected(int num)
{
Feature feature = (Feature) featureListModel.getElementAt(num);
setKeyField(feature.getName());
}
public void featureListSelected()
{
int num = featureList.getSelectedIndex();
if (num < 0)
{
return;
}
Feature feature = (Feature) featureListModel.getElementAt(num);
setKeyField(feature.getName());
//featureDisp
featureDisp(feature);
}
public void findListSelected()
{
int num = findList.getSelectedIndex();
if (num < 0)
{
return;
}
Feature feature = (Feature) findListModel.getElementAt(num);
setKeyField(feature.getName());
//featureDisp
featureDisp(feature);
}
public void indexDisp()
{
try
{
URL url = null;
// System.getProperty("user.dir") +
// System.getProperty("file.separator");
String path = null;
try
{
path = ConstantDefinition.RESOURCEA_PATH + "index.html";
url = getClass().getResource(path);
//System.out.println(url);
} catch (Exception e)
{
System.err.println("Failed to open " + path);
url = null;
}
if (url != null)
{
htmlPane.setPage(url);
}
} catch (MalformedURLException e)
{
System.out.println("Malformed URL: " + e);
} catch (IOException e)
{
System.out.println("IOException: " + e);
}
}
public void featureDisp(Feature feature)
{
String htmlFeature = createHtml(feature);
htmlPane.setContentType("text/html");
htmlPane.setText(htmlFeature); // bug
htmlPane.validate();
System.out.println("feature disp:--" + feature);
}
public void hyperClick(Feature feature)
{
// set textField title
keyField.setText(feature.getName());
// update list selection
int indexClick = featureListModel.indexOf(feature);
featureList.setSelectedIndex(indexClick);
// update content
featureDisp(feature);
}
// index search mode
public int indexSearchList(String key)
{
int indexSearch;
for (indexSearch = 0; indexSearch < featureListModel.size(); indexSearch++)
{
Feature feature = (Feature) featureListModel.getElementAt(indexSearch);
String fname = feature.getName();
if ((key.compareTo(fname) == 0) || (key.compareTo(fname) < 0))
{
break;
}
}
// if the key is behind the last feature name
if (indexSearch == featureListModel.size())
{
indexSearch--;
}
return indexSearch;
}
// find substring mode: add the found feature into the foundlist
public int findSearchList(String key)
{
findListModel.clear();
int indexSearch;
for (indexSearch = 0; indexSearch < featureListModel.size(); indexSearch++)
{
Feature feature = (Feature) featureListModel.getElementAt(indexSearch);
String fname = feature.getName();
if (fname.indexOf(key) > -1)
{
findListModel.addElement(feature);
}
}
return findListModel.size();
}
public void IndexSearchFeature()
{
int listPosition = 0;
listPosition = indexSearchList(keyField.getText());
featureList.setSelectedIndex(listPosition);
Feature sfeature = (Feature) featureListModel.getElementAt(listPosition);
featureDisp(sfeature);
setKeyField(sfeature.getName());
}
public void showFindFeature()
{
featureList.clearSelection();
SwingUtilities.invokeLater(new Runnable() // findList.setSelectedIndex(0); BoxView.java:1079 bug 2003.11.3
{
public void run()
{
findList.setSelectedIndex(0);
}
}
);
Feature sfeature = (Feature) findListModel.getElementAt(0);
featureDisp(sfeature);
//setKeyField(sfeature.getName()); display the key that user type 2003.11.3
}
/// generate html page.
public void setHtmlModel(String fileName)
{
htmlG.setHtmlModel(fileName);
}
public void setHtmlModel(File htmlModel)
{
htmlG.setHtmlModel(htmlModel);
}
public String createHtml(Feature feature)
{
htmlG.initFeature(feature,true);
return htmlG.produceHtml();
}
//GlassPane
private class GlassPane extends JDesktopPane
{
public GlassPane()
{
super();
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
}
public boolean isOpaque()
{
return false;
}
}
/// pane
public TermiInfoPanel getTermiInfoPane()
{
return termiInfoPane;
}
public TermiControlPanel getTerminologyPane()
{
return terminologyPane;
}
/// feature file
public void writeToFile(String filename, String content)
throws Exception
{
FileWriter fw = new FileWriter(filename + ".htm");
fw.write(content);
fw.close();
}
public void saveFile(Feature feature)
{
String content = "";
content = createHtml(feature);
try
{
writeToFile(feature.getName(), content);
} catch (Exception e)
{
System.err.println("saveFile wrong \n");
System.exit(1);
}
}
}