classifiablefilteredit.java
来自「Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI」· Java 代码 · 共 644 行 · 第 1/2 页
JAVA
644 行
update();
// invokeLater prevents a deadlock in jdk <=1.3
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
attributeSelector.setSelectedIndex(-1);
}
});
}
} catch (RaplaException ex) {
showException(ex, getNewComponent());
}
}
}
public ClassificationFilter getFilter() throws RaplaException {
if ( type == null )
return null;
ClassificationFilter filter = type.newClassificationFilter();
int i=0;
for (Iterator it = ruleList.iterator();it.hasNext();) {
RuleComponent ruleComponent = (RuleComponent) it.next();
Attribute attribute = ruleComponent.getAttribute();
List ruleRows = ruleComponent.getRuleRows();
int size = ruleRows.size();
Object[][] conditions = new Object[size][2];
for (int j=0;j<size;j++) {
RuleRow ruleRow = (RuleRow) ruleRows.get(j);
conditions[j][0] = ruleRow.getOperatorValue();
conditions[j][1] = ruleRow.getValue();
}
filter.setRule(i++ , attribute, conditions);
}
return filter;
}
private RuleComponent addRuleComponent() {
RuleComponent ruleComponent = new RuleComponent();
ruleList.add( ruleComponent );
return ruleComponent;
}
private RuleComponent getComponent(Attribute attribute) {
for (Iterator it = ruleList.iterator();it.hasNext();) {
RuleComponent c2 = (RuleComponent)it.next();
if (attribute.equals(c2.getAttribute())) {
return c2;
}
}
return null;
}
private void deleteRule(Component ruleComponent) {
ruleList.remove( ruleComponent );
update();
}
private String getAttName(String key) {
return getName( type.getAttribute(key));
}
class RuleComponent extends JPanel {
private static final long serialVersionUID = 1L;
Attribute attribute;
private Listener listener = new Listener();
List ruleRows = new ArrayList();
List deleteButtons = new ArrayList();
boolean isAndVisible;
JLabel and;
RuleComponent() {
Border outer = BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(5,20,0,3)
,BorderFactory.createEtchedBorder()
);
this.setBorder(BorderFactory.createCompoundBorder(
outer
,BorderFactory.createEmptyBorder(2,3,2,3)
));
this.setOpaque( false );
}
public Attribute getAttribute() {
return attribute;
}
public List getRuleRows() {
return ruleRows;
}
public void newRule(Attribute attribute) throws RaplaException {
ruleRows.clear();
deleteButtons.clear();
this.attribute = attribute;
addRow(null, null);
rebuild();
}
public void setRule(ClassificationFilterRule rule) throws RaplaException {
ruleRows.clear();
deleteButtons.clear();
attribute = rule.getAttribute();
Assert.notNull(attribute);
Object[] ruleValues = rule.getValues();
String[] operators = rule.getOperators();
for (int i=0;i<ruleValues.length;i++) {
addRow(operators[i], ruleValues[i]);
}
rebuild();
}
public void setAndVisible( boolean andVisible) {
this.isAndVisible = andVisible;
if ( and!= null)
{
if ( andVisible) {
and.setText( getString("and"));
} else {
and.setText("");
}
}
}
private void rebuild() {
this.removeAll();
TableLayout layout = new TableLayout();
layout.insertColumn(0,TableLayout.PREFERRED);
layout.insertColumn(1,10);
layout.insertColumn(2,TableLayout.PREFERRED);
layout.insertColumn(3,5);
layout.insertColumn(4,TableLayout.PREFERRED);
layout.insertColumn(5,5);
layout.insertColumn(6,TableLayout.FILL);
this.setLayout(layout);
int row =0;
layout.insertRow(row,TableLayout.PREFERRED);
and = new JLabel();
// and.setAlignmentX( and.LEFT_ALIGNMENT);
this.add("0,"+row +",6,"+ row + ",l,c", and);
if ( isAndVisible) {
and.setText( getString("and"));
} else {
and.setText("");
}
row ++;
int size = ruleRows.size();
for (int i=0;i<size;i++) {
RuleRow ruleRow = (RuleRow) ruleRows.get(i);
RaplaButton deleteButton = (RaplaButton) deleteButtons.get(i);
layout.insertRow(row,TableLayout.PREFERRED);
this.add("0," + row + ",l,c", deleteButton);
if (i == 0)
this.add("2," + row + ",l,c", ruleRow.ruleLabel);
else
this.add("2," + row + ",r,c", new JLabel(getString("or")));
this.add("4," + row + ",l,c", ruleRow.operatorComponent);
this.add("6," + row + ",f,c", ruleRow.field.getComponent());
row ++;
if (i<size -1) {
layout.insertRow(row , 2);
row++;
}
}
revalidate();
repaint();
}
public void addOr() throws RaplaException{
addRow(null,null);
rebuild();
}
private void addRow(String operator,Object ruleValue) throws RaplaException {
RaplaButton deleteButton = new RaplaButton(RaplaButton.SMALL);
deleteButton.setToolTipText(getString("delete"));
deleteButton.setIcon(getIcon("icon.delete"));
deleteButton.addActionListener(listener);
deleteButtons.add(deleteButton);
ruleRows.add(new RuleRow(attribute,operator,ruleValue));
}
class Listener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
int index = deleteButtons.indexOf(evt.getSource());
if (ruleRows.size() <= 1) {
deleteRule(RuleComponent.this);
} else {
ruleRows.remove(index);
deleteButtons.remove(index);
rebuild();
}
}
}
}
class RuleRow {
Object ruleValue;
JLabel ruleLabel;
JComponent operatorComponent;
AbstractEditField field;
Attribute attribute;
RuleRow(Attribute attribute,String operator,Object ruleValue) throws RaplaException {
this.attribute = attribute;
this.ruleValue = ruleValue;
ruleLabel = new JLabel();
ruleLabel.setText(attribute.getName().getName(getI18n().getLang()));
createField( attribute );
field.setValue(ruleValue);
setOperatorValue(operator);
}
public String getOperatorValue() {
AttributeType type = attribute.getType();
if (type.equals(AttributeType.CATEGORY) || type.equals(AttributeType.BOOLEAN) )
return "is";
if (type.equals(AttributeType.STRING))
return "contains";
if (type.equals(AttributeType.DATE) || type.equals(AttributeType.INT)) {
int index = ((JComboBox)operatorComponent).getSelectedIndex();
if (index == 0)
return "<";
if (index == 1)
return "=";
if (index == 2)
return ">";
}
Assert.notNull(field,"Unknown AttributeType" + type);
return null;
}
private void setOperatorValue(String operator) {
AttributeType type = attribute.getType();
if ((type.equals(AttributeType.DATE) || type.equals(AttributeType.INT)))
{
if (operator == null)
operator = "<";
JComboBox box = (JComboBox)operatorComponent;
if (operator.equals("<"))
box.setSelectedIndex(0);
if (operator.equals("="))
box.setSelectedIndex(1);
if (operator.equals(">"))
box.setSelectedIndex(2);
}
}
private EditField createField(Attribute attribute) throws RaplaException {
operatorComponent = null;
AttributeType type = attribute.getType();
String key = attribute.getKey();
if (type.equals(AttributeType.CATEGORY))
{
operatorComponent = new JLabel("");
Category rootCategory = (Category)attribute.getConstraint(ConstraintIds.KEY_ROOT_CATEGORY);
if (rootCategory.getDepth() > 2) {
field = new CategorySelectField(getContext(),key,rootCategory);
} else {
field = new CategoryListField(getContext(),key,rootCategory);
}
}
else if (type.equals(AttributeType.STRING))
{
operatorComponent = new JLabel(getString("filter.contains"));
field = new TextField(getContext(),key);
}
else if (type.equals(AttributeType.INT))
{
field = new LongField(getContext(),key);
DefaultComboBoxModel model = new DefaultComboBoxModel(new String[] {
getString("filter.is_smaller_than")
,getString("filter.equals")
,getString("filter.is_bigger_than")});
operatorComponent = new JComboBox(model);
}
else if (type.equals(AttributeType.DATE))
{
field = new DateField(getContext(),key);
DefaultComboBoxModel model = new DefaultComboBoxModel(new String[] {
getString("filter.earlier_than")
,getString("filter.equals")
,getString("filter.later_than")});
operatorComponent = new JComboBox(model);
}
else if (type.equals(AttributeType.BOOLEAN))
{
operatorComponent = new JLabel("");
field = new BooleanField(getContext(),key);
ruleValue = new Boolean(false);
}
Assert.notNull(field,"Unknown AttributeType");
field.setDelegate(new MyMappingDelegate(field));
return field;
}
public Object getValue() throws RaplaException {
ruleValue = field.getValue();
return ruleValue;
}
class MyMappingDelegate implements MappingDelegate {
AbstractEditField editField;
MyMappingDelegate(AbstractEditField field) {
this.editField = field;
}
public String getName() {
return getAttName(editField.getFieldName());
}
public void mapFrom(Object o) throws RaplaException {
}
public void mapTo(Object o) throws RaplaException {
}
};
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?