package de.FeatureModellingTool.DictEditor.TermiPanelEditor;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.TreeMap;
import javax.swing.event.*;
import de.FeatureModellingTool.DictEditor.TerminologyModel.Terminology;
import de.FeatureModellingTool.DictEditor.TerminologyModel.TermiConstants;
import de.FeatureModellingTool.DictEditor.event.TermiPropertyChangeListener;
import de.FeatureModellingTool.DictEditor.event.TermiPropertyChangeEvent;
/**
* Recoder: Raymond
* Date: 2003-10-10
* Time: 14:10:58
*/
public class TermiInfoPanel extends JPanel implements TermiPropertyChangeListener
{
protected JPanel basicAttributePanel = null;
private Color background = new Color(236,233,216);
boolean editMode = true;
protected TermiField name = null;
protected TermiField source = null;
protected TermiField synonym = null; // for demo
JButton newTermi = null;
JButton delTermi = null;
JRadioButton readOnly = null;
JRadioButton editable = null;
protected TermiDescriptionPanel descriptionPanel = null;
private Component container = null;
private final EventListenerList listenerList = new EventListenerList();
protected Terminology terminology = null;
protected TreeMap editMemory = new TreeMap();
public TermiInfoPanel()
{
setFont(new Font("Dialog", Font.PLAIN, 12));
init();
assembleGUIComponent();
setEditable(false);
newTermi.setEnabled(false);
delTermi.setEnabled(false);
editable.setSelected(true);
readOnly.addItemListener
(
new ItemListener()
{
public void itemStateChanged (ItemEvent event)
{
if (readOnly.isSelected())
{
editMode= false;
setEditable(false);
}
else
{
editMode= true;
setEditable(true);
}
}
}
);
}
public void setContainer(Component container)
{
this.container = container;
}
private Component getContainer()
{
return container;
}
protected JButton getNewTermi()
{
return newTermi;
}
protected JButton getDelTermi()
{
return delTermi;
}
protected TermiField getNameField()
{
return name;
}
protected void init()
{
name = new TermiField(TermiConstants.NAME);
source = new TermiField(TermiConstants.SOURCE);
synonym = new TermiField(TermiConstants.SYNONYM);
}
protected void assembleGUIComponent()
{
createBasicAttributePanel();
createDescriptionPanel();
createRegularPane();
}
protected void createBasicAttributePanel()
{
basicAttributePanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
basicAttributePanel.setLayout(gbl);
// top margin
gbc.gridwidth = GridBagConstraints.REMAINDER;
basicAttributePanel.add(Box.createVerticalStrut(15), gbc);
//row 1
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 1;
basicAttributePanel.add(Box.createHorizontalStrut(10), gbc);
basicAttributePanel.add(new JLabel("Name"), gbc);
basicAttributePanel.add(Box.createHorizontalStrut(15), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
basicAttributePanel.add(name, gbc);
gbc.weightx = 0;
basicAttributePanel.add(Box.createVerticalStrut(7), gbc);
//row 2
gbc.gridwidth = 1;
basicAttributePanel.add(Box.createHorizontalStrut(10), gbc);
basicAttributePanel.add(new JLabel("Source"), gbc);
basicAttributePanel.add(Box.createHorizontalStrut(15), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
basicAttributePanel.add(source, gbc);
gbc.weightx = 0;
basicAttributePanel.add(Box.createVerticalStrut(7), gbc);
//row 3
gbc.gridwidth = 1;
basicAttributePanel.add(Box.createHorizontalStrut(10), gbc);
basicAttributePanel.add(new JLabel("Synonym"), gbc);
basicAttributePanel.add(Box.createHorizontalStrut(15), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
//JTextField synonym = new JTextField(20);
//Color background = new Color(236,233,216);
//synonym.setBackground(background);
basicAttributePanel.add(synonym, gbc);
gbc.weightx = 0;
basicAttributePanel.add(Box.createVerticalStrut(10), gbc);
}
protected void createDescriptionPanel()
{
descriptionPanel = new TermiDescriptionPanel();
}
protected void createRegularPane()
{
// controlPane
readOnly =new JRadioButton("Read Only");
editable =new JRadioButton("Editable");
JLabel modelLabel = new JLabel("mode :");
ButtonGroup modeGroup = new ButtonGroup();
modeGroup.add(readOnly);
modeGroup.add(editable);
JPanel controlPane = new JPanel(new GridBagLayout());
GridBagConstraints cgbc = new GridBagConstraints();
// controlPane top margin
cgbc.gridwidth = GridBagConstraints.REMAINDER;
controlPane.add(Box.createVerticalStrut(5), cgbc);
cgbc.anchor = GridBagConstraints.NORTHWEST;
cgbc.gridwidth = 1 ;
controlPane.add(Box.createHorizontalStrut(55), cgbc);
controlPane.add(readOnly,cgbc);
cgbc.weightx = 0;
cgbc.gridwidth = GridBagConstraints.REMAINDER;
controlPane.add(editable,cgbc);
cgbc.weightx = 0;
controlPane.add(Box.createVerticalStrut(2), cgbc);
//JPanel one
JPanel one = new JPanel();
one.setLayout(new BorderLayout());
one.setBorder(BorderFactory.createTitledBorder("--basic"));
//one.setMinimumSize(new Dimension(250,120));
one.add(basicAttributePanel, BorderLayout.CENTER);
//JPanel two
JPanel two = new JPanel();
two.setLayout(new BorderLayout());
two.setBorder(BorderFactory.createTitledBorder("--description"));
//two.setMinimumSize(new Dimension(250,380));
two.add(descriptionPanel, BorderLayout.CENTER);
//regularPane
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
newTermi = new JButton("NEW Terminology");
delTermi = new JButton("DEL Terminology");
//row 1
gbc.gridwidth = 1 ;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(controlPane,gbc);
//row 2
gbc.weightx=2;
gbc.gridwidth = 1 ;
add(Box.createHorizontalStrut(20), gbc);
gbc.weightx=0;
add(newTermi, gbc);
gbc.gridwidth = 1 ;
gbc.weightx=1;
add(Box.createHorizontalStrut(5), gbc);
gbc.weightx=0;
add(delTermi, gbc);
gbc.weightx=2;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(Box.createHorizontalStrut(5), gbc);
//row 3
gbc.gridx = 0 ;
add(Box.createVerticalStrut(25), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(one, gbc);
gbc.weighty = 0.3 ;
add(Box.createVerticalStrut(15), gbc);
gbc.weightx = 1 ;
gbc.weighty = 1 ;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(two, gbc);
}
protected void setEnableTermiPropertyChangeEvent(boolean option)
{
name.setEnableTermiPropertyChangeEvent(option);
source.setEnableTermiPropertyChangeEvent(option);
synonym.setEnableTermiPropertyChangeEvent(option);
descriptionPanel.setEnableTermiPropertyChangeEvent(option);
}
protected void setEditable(boolean option)
{
if(editMode)
{
name.setEditable(option);
source.setEditable(option);
synonym.setEditable(option);
descriptionPanel.setEditable(option);
}
else
{
option=false;
name.setEditable(option);
source.setEditable(option);
synonym.setEditable(option);
descriptionPanel.setEditable(option);
}
}
public void setTerminology(Terminology terminology)
{
if (this.terminology == terminology)
return;
setEnableTermiPropertyChangeEvent(false);
Terminology old = null;
if (this.terminology!=null)
{
old = this.terminology;
}
this.terminology = terminology;
//store old Termi's description attribute if it is editing now
if( (old != null) && descriptionPanel.isEditing())
{
TermiEditStateInfo memoryInfo = (TermiEditStateInfo) editMemory.get(old.getID());
if(memoryInfo != null)
{
memoryInfo.setCurrentDescription(descriptionPanel.getUnsubmitDescription());
}
else
{
TermiEditStateInfo info = new TermiEditStateInfo(old.getID(), descriptionPanel.getUnsubmitDescription(), true);
editMemory.put(old.getID(), info);
}
}
if (terminology != null)
{
if (old == null)
setEditable(true);
TermiEditStateInfo memoryInfo = (TermiEditStateInfo) editMemory.get(terminology.getID());
name.setText(terminology.getName());
source.setText(terminology.getSource());
synonym.setText(terminology.getSynonym());
descriptionPanel.setDescription(terminology.getDescription());
if(memoryInfo == null)
{
descriptionPanel.setEditing(false);
}
else
{
descriptionPanel.setUnsubmitDescription(memoryInfo.getCurrentDescription());
descriptionPanel.setEditing(true);
}
setEnableTermiPropertyChangeEvent(true);
}
else
{
setEditable(false);
name.setText("");
source.setText("");
synonym.setText("");
descriptionPanel.setDescription("");
}
}
public void propertyChange(TermiPropertyChangeEvent evt)
{
String src = (String)evt.getSource();
if ( (terminology == null) || !terminology.getID().equals(src) )
return;
if ( evt.getPropertyName().equals(TermiConstants.NAME) )
{
setEnableTermiPropertyChangeEvent(false);
name.setText(terminology.getName());
setEnableTermiPropertyChangeEvent(true);
}
else
{
System.out.println("In current time, no process prepared for property " + evt.getPropertyName() +"change event");
}
}
public void addTermiPropertyChangeListener(TermiPropertyChangeListener listener)
{
listenerList.add(TermiPropertyChangeListener.class, listener);
}
public void removeTermiPropertyChangeListener(TermiPropertyChangeListener listener)
{
listenerList.remove(TermiPropertyChangeListener.class, listener);
}
protected void fireTermiPropertyChangeEvent(TermiPropertyChangeEvent event)
{
final Object[] listeners = listenerList.getListenerList();
TermiPropertyChangeListener listener = null;
for (int i = listeners.length-2; i>=0 ; i-=2)
{
if (listeners[i] == TermiPropertyChangeListener.class)
{
listener = (TermiPropertyChangeListener)listeners[i+1];
listener.propertyChange(event);
}
}
}
private class TermiField extends JTextField
{
protected DocumentListener fnListener = new DocumentListener()
{
public void insertUpdate(DocumentEvent e)
{
dispatchEvent();
}
public void removeUpdate(DocumentEvent e)
{
dispatchEvent();
}
public void changedUpdate(DocumentEvent e)
{
dispatchEvent();
}
};
protected FocusListener fcListener = new FocusListener()
{
public void focusGained(FocusEvent e)
{
TermiField termifield = (TermiField)e.getSource();
if(termifield.isEditable())
termifield.setBackground(Color.white);
}
public void focusLost(FocusEvent e)
{
TermiField termifield = (TermiField)e.getSource();
termifield.setBackground(background);
}
};
protected boolean enableTermiPropertyChangeEvent = false;
protected String termiProperty = null;
public TermiField(String termiProperty)
{
super(18);
this.termiProperty = termiProperty;
this.setMargin(new Insets(0,3,0,0));
this.getDocument().addDocumentListener(fnListener);
this.addFocusListener(fcListener);
}
public void setEnableTermiPropertyChangeEvent(boolean option)
{
if (option != enableTermiPropertyChangeEvent)
{
enableTermiPropertyChangeEvent = option;
}
}
private void dispatchEvent()
{
if (enableTermiPropertyChangeEvent == false)
return;
fireTermiPropertyChangeEvent
(
new TermiPropertyChangeEvent
(
terminology.getID(),
termiProperty,
null,
this.getText()
)
);
this.requestFocus();
}
}
private class TermiDescriptionPanel extends JPanel
{
protected JTextArea descriptionArea = null;
protected JButton start = null;
protected JButton conform = null;
protected JButton cancel = null;
protected JScrollPane scrollPane = null;
protected JPanel controlPane = null;
protected String description = "";
protected boolean enableTermiPropertyChangeEvent = false;
protected boolean editable = false;
public void setEnableTermiPropertyChangeEvent(boolean option)
{
if (option != enableTermiPropertyChangeEvent)
{
enableTermiPropertyChangeEvent = option;
}
}
public boolean isEditing()
{
return !start.isEnabled();
}
public void setEditing(boolean editing)
{
if(!isEditable())
return;
if(editing == true)
{
start.setEnabled(false);
conform.setEnabled(true);
cancel.setEnabled(true);
descriptionArea.setEditable(true);
descriptionArea.setBackground(Color.white);
}
else
{
start.setEnabled(true);
conform.setEnabled(false);
cancel.setEnabled(false);
descriptionArea.setEditable(false);
descriptionArea.setBackground(background);
}
}
public boolean isEditable()
{
return editable;
}
public void setEditable(boolean option)
{
editable = option;
if (option == true)
{
start.setEnabled(true);
descriptionArea.setEditable(false);
descriptionArea.setBackground(background);
}
else
{
start.setEnabled(false);
conform.setEnabled(false);
cancel.setEnabled(false);
descriptionArea.setEditable(false);
descriptionArea.setBackground(background);
}
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
setUnsubmitDescription(description);
}
public void setUnsubmitDescription(String description)
{
descriptionArea.setText(description);
}
public String getUnsubmitDescription()
{
return descriptionArea.getText();
}
public TermiDescriptionPanel()
{
//1
descriptionArea = new JTextArea(15,28);
scrollPane = new JScrollPane();
descriptionArea.setLineWrap(true);
descriptionArea.setWrapStyleWord(false);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setViewportView(descriptionArea);
//2
start = new JButton("Modify");
conform = new JButton("Commit");
cancel = new JButton("Cancel");
controlPane = new JPanel();
controlPane.setLayout(new FlowLayout());
controlPane.add(start);
controlPane.add(conform);
controlPane.add(cancel);
//3
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(controlPane, BorderLayout.SOUTH);
//4
descriptionArea.setEditable(false);
descriptionArea.setBackground(background);
start.setEnabled(false);
conform.setEnabled(false);
cancel.setEnabled(false);
start.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
start.setEnabled(false);
conform.setEnabled(true);
cancel.setEnabled(true);
descriptionArea.setBackground(Color.white);
descriptionArea.setEditable(true);
}
}
);
conform.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
conform.setEnabled(false);
cancel.setEnabled(false);
descriptionArea.setEditable(false);
descriptionArea.setBackground(background);
description = descriptionArea.getText();
//fire description change event
dispatchEvent();
//remove from edit mempry
editMemory.remove(terminology.getID());
start.setEnabled(true);
}
}
);
cancel.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
cancel.setEnabled(false);
conform.setEnabled(false);
descriptionArea.setEditable(false);
descriptionArea.setBackground(background);
descriptionArea.setText(description);
//remove from edit mempry
editMemory.remove(terminology.getID());
start.setEnabled(true);
}
}
);
}
private void dispatchEvent()
{
if (enableTermiPropertyChangeEvent == false)
return;
fireTermiPropertyChangeEvent
(
new TermiPropertyChangeEvent
(
terminology.getID(),
TermiConstants.DESCRIPTION,
null,
description
)
);
}
}
protected class TermiEditStateInfo
{
protected String terminologyID = null;
protected String currentDescription = null;
protected boolean isEditing = false;
public TermiEditStateInfo(String terminologyID, String currentDescription, boolean isEditing)
{
this.terminologyID = terminologyID;
this.currentDescription = currentDescription;
this.isEditing = isEditing;
}
public void setTerminologyID(String terminologyID)
{
this.terminologyID = terminologyID;
}
public void setCurrentDescription(String currentDescription)
{
this.currentDescription = currentDescription;
}
public void setEditing(boolean isEditing)
{
this.isEditing = isEditing;
}
public String getTerminologyID()
{
return terminologyID;
}
public String getCurrentDescription()
{
return currentDescription;
}
public boolean isEditing()
{
return isEditing;
}
}
}