private void refresh() { if (inUseModel.getSize() > 0) { piece = (GamePiece) inUseModel.lastElement(); } else { piece = null; } slot.setPiece(piece); slot.getComponent().repaint(); }
/** * This method is called from within the constructor to initialize the form. WARNING: Do NOT * modify this code. The content of this method is always regenerated by the FormEditor. */ private void initComponents() { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); add(slot.getComponent()); JPanel controls = new JPanel(); controls.setLayout(new BoxLayout(controls, BoxLayout.X_AXIS)); availablePanel = new JPanel(); availableScroll = new JScrollPane(); availableList = new JList(); helpButton = new JButton(); importButton = new JButton(); addRemovePanel = new JPanel(); addButton = new JButton(); removeButton = new JButton(); inUsePanel = new JPanel(); inUseScroll = new JScrollPane(); inUseList = new JList(); propsButton = new JButton(); moveUpDownPanel = new JPanel(); moveUpButton = new JButton(); moveDownButton = new JButton(); copyButton = new JButton(); pasteButton = new JButton(); // setLayout(new BoxLayout(this, 0)); availablePanel.setLayout(new BoxLayout(availablePanel, 1)); availableList.setModel(availableModel); availableList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); availableList.setCellRenderer(r); availableList.addListSelectionListener( new ListSelectionListener() { public void valueChanged(ListSelectionEvent evt) { Object o = availableList.getSelectedValue(); helpButton.setEnabled( o instanceof EditablePiece && ((EditablePiece) o).getHelpFile() != null); addButton.setEnabled(o instanceof Decorator); } }); availableScroll.setViewportView(availableList); availableScroll.setBorder(new TitledBorder("Available Traits")); availablePanel.add(availableScroll); helpButton.setText("Help"); helpButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { showHelpForPiece(); } }); availablePanel.add(helpButton); importButton.setText("Import"); importButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { String className = JOptionPane.showInputDialog( PieceDefiner.this, "Enter fully-qualified name of Java class to import"); importPiece(className); } }); availablePanel.add(importButton); controls.add(availablePanel); addRemovePanel.setLayout(new BoxLayout(addRemovePanel, 1)); addButton.setText("Add ->"); addButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { Object selected = availableList.getSelectedValue(); if (selected instanceof Decorator) { if (inUseModel.getSize() > 0) { Decorator c = (Decorator) selected; addTrait(c); if (inUseModel.lastElement().getClass() == c.getClass()) { if (edit(inUseModel.size() - 1)) { // Add was successful } else { // Add was cancelled if (!inUseModel.isEmpty()) removeTrait(inUseModel.size() - 1); } } } } else if (selected instanceof GamePiece && inUseModel.getSize() == 0) { GamePiece p = null; try { p = (GamePiece) selected.getClass().getConstructor().newInstance(); } catch (Throwable t) { ReflectionUtils.handleNewInstanceFailure(t, selected.getClass()); } if (p != null) { setPiece(p); if (inUseModel.getSize() > 0) { if (edit(0)) { // Add was successful } else { // Add was cancelled removeTrait(0); } } } } } }); addButton.setAlignmentX(Component.CENTER_ALIGNMENT); addRemovePanel.add(addButton); removeButton.setText("<- Remove"); removeButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { int index = inUseList.getSelectedIndex(); if (index >= 0) { removeTrait(index); if (inUseModel.getSize() > 0) { inUseList.setSelectedIndex(Math.min(inUseModel.getSize() - 1, Math.max(index, 0))); } } } }); removeButton.setAlignmentX(Component.CENTER_ALIGNMENT); addRemovePanel.add(removeButton); pieceIdLabel.setAlignmentX(Component.CENTER_ALIGNMENT); addRemovePanel.add(pieceIdLabel); controls.add(addRemovePanel); inUsePanel.setLayout(new BoxLayout(inUsePanel, 1)); inUseList.setModel(inUseModel); inUseList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); inUseList.setCellRenderer(r); inUseList.addListSelectionListener( new ListSelectionListener() { public void valueChanged(ListSelectionEvent evt) { Object o = inUseList.getSelectedValue(); int index = inUseList.getSelectedIndex(); propsButton.setEnabled(o instanceof EditablePiece); if (inUseModel.size() > 1) { removeButton.setEnabled(index > 0); copyButton.setEnabled(index > 0); } else if (inUseModel.size() == 1) { removeButton.setEnabled( index == 0 && !(inUseModel.getElementAt(0) instanceof BasicPiece)); copyButton.setEnabled(index == 0); } else { removeButton.setEnabled(false); copyButton.setEnabled(false); } copyButton.setEnabled(index > 0); pasteButton.setEnabled(clipBoard != null); moveUpButton.setEnabled(index > 1); moveDownButton.setEnabled(index > 0 && index < inUseModel.size() - 1); } }); inUseList.addMouseListener( new MouseAdapter() { public void mouseReleased(MouseEvent evt) { if (evt.getClickCount() == 2) { int index = inUseList.locationToIndex(evt.getPoint()); if (index >= 0) { edit(index); } } } }); inUseScroll.setViewportView(inUseList); inUseScroll.setBorder(new TitledBorder("Current Traits")); inUsePanel.add(inUseScroll); propsButton.setText("Properties"); propsButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { int index = inUseList.getSelectedIndex(); if (index >= 0) { edit(index); } } }); inUsePanel.add(propsButton); controls.add(inUsePanel); moveUpDownPanel.setLayout(new BoxLayout(moveUpDownPanel, BoxLayout.Y_AXIS)); moveUpButton.setText("Move Up"); moveUpButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { int index = inUseList.getSelectedIndex(); if (index > 1 && index < inUseModel.size()) { moveDecoratorUp(index); } } }); moveUpDownPanel.add(moveUpButton); moveDownButton.setText("Move Down"); moveDownButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { int index = inUseList.getSelectedIndex(); if (index > 0 && index < inUseModel.size() - 1) { moveDecoratorDown(index); } } }); moveUpDownPanel.add(moveDownButton); copyButton.setText("Copy"); copyButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { pasteButton.setEnabled(true); int index = inUseList.getSelectedIndex(); clipBoard = new TraitClipboard((Decorator) inUseModel.get(index)); } }); moveUpDownPanel.add(copyButton); pasteButton.setText("Paste"); pasteButton.setEnabled(clipBoard != null); pasteButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { if (clipBoard != null) { paste(); } } }); moveUpDownPanel.add(pasteButton); controls.add(moveUpDownPanel); add(controls); }