attributeedit.java
来自「Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI」· Java 代码 · 共 438 行 · 第 1/2 页
JAVA
438 行
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.gui.internal.edit;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.rapla.components.calendar.RaplaNumber;
import org.rapla.components.layout.TableLayout;
import org.rapla.entities.Category;
import org.rapla.entities.dynamictype.Attribute;
import org.rapla.entities.dynamictype.AttributeAnnotations;
import org.rapla.entities.dynamictype.AttributeType;
import org.rapla.entities.dynamictype.ConstraintIds;
import org.rapla.entities.dynamictype.DynamicType;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.RaplaGUIComponent;
import org.rapla.gui.toolkit.EmptyLineBorder;
import org.rapla.gui.toolkit.RaplaWidget;
/**
* @author Christopher Kohlhaas
*/
public class AttributeEdit extends RaplaGUIComponent
implements
RaplaWidget
{
RaplaListEdit listEdit;
DynamicType dt;
DefaultConstraints constraintPanel;
ArrayList listenerList = new ArrayList();
Listener listener = new Listener();
DefaultListModel model = new DefaultListModel();
boolean editKeys;
public AttributeEdit(RaplaContext sm) throws RaplaException {
super( sm);
constraintPanel = new DefaultConstraints(sm);
listEdit = new RaplaListEdit( getI18n(), constraintPanel.getComponent(), listener );
listEdit.setListDimension( new Dimension( 200,220 ) );
constraintPanel.addChangeListener( listener );
listEdit.getComponent().setBorder( BorderFactory.createTitledBorder( new EmptyLineBorder(),getString("attributes")) );
listEdit.getList().setCellRenderer(new DefaultListCellRenderer() {
private static final long serialVersionUID = 1L;
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
Attribute a = (Attribute) value;
value = a.getName(getRaplaLocale().getLocale());
if (editKeys) {
value = "{" + a.getKey() + "} " + value;
}
value = (index + 1) +") " + value;
return super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus);
}
});
constraintPanel.setEditKeys( false );
}
public RaplaWidget getConstraintPanel() {
return constraintPanel;
}
class Listener implements ActionListener,ChangeListener {
public void actionPerformed(ActionEvent evt) {
int index = getSelectedIndex();
try {
if (evt.getActionCommand().equals("remove")) {
removeAttribute();
} else if (evt.getActionCommand().equals("new")) {
createAttribute();
} else if (evt.getActionCommand().equals("edit")) {
constraintPanel.mapFrom( listEdit.getList().getSelectedValue() );
} else if (evt.getActionCommand().equals("moveUp")) {
dt.exchangeAttributes(index, index -1);
updateModel();
} else if (evt.getActionCommand().equals("moveDown")) {
dt.exchangeAttributes(index, index + 1);
updateModel();
}
} catch (RaplaException ex) {
showException(ex, getComponent());
}
}
public void stateChanged(ChangeEvent e) {
try {
confirmEdits();
fireContentChanged();
} catch (RaplaException ex) {
showException(ex, getComponent());
}
}
}
public JComponent getComponent() {
return listEdit.getComponent();
}
public int getSelectedIndex() {
return listEdit.getList().getSelectedIndex();
}
public void setDynamicType(DynamicType dt) throws RaplaException {
this.dt = dt;
updateModel();
}
private void updateModel() throws RaplaException {
Object selectedItem = listEdit.getList().getSelectedValue();
model.clear();
Attribute[] attributes = dt.getAttributes();;
for (int i = 0; i < attributes.length; i++ ) {
model.addElement( attributes[i] );
}
listEdit.getList().setModel(model);
if ( listEdit.getList().getSelectedValue() != selectedItem )
listEdit.getList().setSelectedValue(selectedItem, true );
}
public void confirmEdits() throws RaplaException {
if ( getSelectedIndex() < 0 )
return;
Object attribute = listEdit.getList().getSelectedValue();
constraintPanel.mapTo ( attribute );
model.set( model.indexOf( attribute ), attribute );
}
public void setEditKeys(boolean editKeys) {
constraintPanel.setEditKeys(editKeys);
this.editKeys = editKeys;
}
private String createNewKey() {
Attribute[] atts = dt.getAttributes();
int max = 1;
for (int i=0;i<atts.length;i++) {
String key = atts[i].getKey();
if (key.length()>1
&& key.charAt(0) =='a'
&& Character.isDigit(key.charAt(1))
)
{
try {
int value = Integer.valueOf(key.substring(1)).intValue();
if (value >= max)
max = value + 1;
} catch (NumberFormatException ex) {
}
}
}
return "a" + (max);
}
void removeAttribute() throws RaplaException {
int index = getSelectedIndex();
Attribute att = dt.getAttributes() [index];
dt.removeAttribute(att);
updateModel();
}
void createAttribute() throws RaplaException {
confirmEdits();
AttributeType type = AttributeType.STRING;
Attribute att = (Attribute) getModification().newAttribute(type);
att.setKey(createNewKey());
dt.addAttribute(att);
updateModel();
listEdit.getList().setSelectedIndex( dt.getAttributes().length -1 );
}
public void addChangeListener(ChangeListener listener) {
listenerList.add(listener);
}
public void removeChangeListener(ChangeListener listener) {
listenerList.remove(listener);
}
public ChangeListener[] getChangeListeners() {
return (ChangeListener[])listenerList.toArray(new ChangeListener[]{});
}
protected void fireContentChanged() {
if (listenerList.size() == 0)
return;
ChangeEvent evt = new ChangeEvent(this);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?