Search in sources :

Example 1 with UnconditionalRunningStrategy

use of gate.creole.RunningStrategy.UnconditionalRunningStrategy in project gate-core by GateNLP.

the class SerialControllerEditor method initListeners.

// initGuiComponents()
protected void initListeners() {
    Gate.getCreoleRegister().addCreoleListener(this);
    this.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            processMouseEvent(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            processMouseEvent(e);
        }

        protected void processMouseEvent(MouseEvent e) {
            if (e.isPopupTrigger()) {
                // context menu
                if (handle != null && handle.getPopup() != null) {
                    handle.getPopup().show(SerialControllerEditor.this, e.getX(), e.getY());
                }
            }
        }
    });
    moveUpButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            int[] rows = memberPRsTable.getSelectedRows();
            if (rows == null || rows.length == 0) {
                JOptionPane.showMessageDialog(SerialControllerEditor.this, "Please select some components to be moved " + "from the list of used components!\n", "GATE", JOptionPane.ERROR_MESSAGE);
            } else {
                // we need to make sure the rows are sorted
                Arrays.sort(rows);
                // get the list of PRs
                for (int row : rows) {
                    if (row > 0) {
                        // move it up
                        List<RunningStrategy> strategies = null;
                        if (conditionalMode) {
                            strategies = new ArrayList<RunningStrategy>(((ConditionalController) controller).getRunningStrategies());
                            RunningStrategy straegy = strategies.remove(row);
                            strategies.add(row - 1, straegy);
                        }
                        ProcessingResource value = controller.remove(row);
                        controller.add(row - 1, value);
                        if (conditionalMode) {
                            ((ConditionalController) controller).setRunningStrategies(strategies);
                            ;
                        }
                    }
                }
                // restore selection
                for (int row : rows) {
                    int newRow;
                    if (row > 0)
                        newRow = row - 1;
                    else
                        newRow = row;
                    memberPRsTable.addRowSelectionInterval(newRow, newRow);
                }
                memberPRsTable.requestFocusInWindow();
            }
        }
    });
    moveDownButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            int[] rows = memberPRsTable.getSelectedRows();
            if (rows == null || rows.length == 0) {
                JOptionPane.showMessageDialog(SerialControllerEditor.this, "Please select some components to be moved " + "from the list of used components!\n", "GATE", JOptionPane.ERROR_MESSAGE);
            } else {
                // we need to make sure the rows are sorted
                Arrays.sort(rows);
                // get the list of PRs
                for (int i = rows.length - 1; i >= 0; i--) {
                    int row = rows[i];
                    if (row < controller.getPRs().size() - 1) {
                        List<RunningStrategy> strategies = null;
                        if (conditionalMode) {
                            strategies = new ArrayList<RunningStrategy>(((ConditionalController) controller).getRunningStrategies());
                            RunningStrategy straegy = strategies.remove(row);
                            strategies.add(row + 1, straegy);
                        }
                        // move it down
                        ProcessingResource value = controller.remove(row);
                        controller.add(row + 1, value);
                        if (conditionalMode) {
                            ((ConditionalController) controller).setRunningStrategies(strategies);
                            ;
                        }
                    }
                }
                // restore selection
                for (int row : rows) {
                    int newRow;
                    if (row < controller.getPRs().size() - 1)
                        newRow = row + 1;
                    else
                        newRow = row;
                    memberPRsTable.addRowSelectionInterval(newRow, newRow);
                }
                memberPRsTable.requestFocusInWindow();
            }
        }
    });
    // mouse click edit the resource
    // mouse double click or context menu add the resource to the application
    loadedPRsTable.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                processMouseEvent(e);
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                processMouseEvent(e);
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            processMouseEvent(e);
        }

        protected void processMouseEvent(MouseEvent e) {
            int row = loadedPRsTable.rowAtPoint(e.getPoint());
            if (row == -1) {
                return;
            }
            if (e.isPopupTrigger()) {
                // context menu
                if (!loadedPRsTable.isRowSelected(row)) {
                    // if right click outside the selection then reset selection
                    loadedPRsTable.getSelectionModel().setSelectionInterval(row, row);
                }
                JPopupMenu popup = new XJPopupMenu();
                popup.add(addPRAction);
                popup.show(loadedPRsTable, e.getPoint().x, e.getPoint().y);
            } else if (e.getID() == MouseEvent.MOUSE_CLICKED) {
                if (e.getClickCount() == 2) {
                    // add selected modules on double click
                    addPRAction.actionPerformed(null);
                }
            }
        }
    });
    // drag and drop support
    loadedPRsTable.setTransferHandler(new TransferHandler() {

        // minimal drag and drop that only call the removePRAction when importing
        String source = "";

        @Override
        public int getSourceActions(JComponent c) {
            return MOVE;
        }

        @Override
        protected Transferable createTransferable(JComponent c) {
            return new StringSelection("loadedPRsTable");
        }

        @Override
        protected void exportDone(JComponent c, Transferable data, int action) {
        }

        @Override
        public boolean canImport(JComponent c, DataFlavor[] flavors) {
            for (DataFlavor flavor : flavors) {
                if (DataFlavor.stringFlavor.equals(flavor)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public boolean importData(JComponent c, Transferable t) {
            if (canImport(c, t.getTransferDataFlavors())) {
                try {
                    source = (String) t.getTransferData(DataFlavor.stringFlavor);
                    if (source.startsWith("memberPRsTable")) {
                        removePRAction.actionPerformed(null);
                        return true;
                    } else {
                        return false;
                    }
                } catch (UnsupportedFlavorException ufe) {
                // just return false later
                } catch (IOException ioe) {
                // just return false later
                }
            }
            return false;
        }
    });
    // mouse click edit the resource
    // mouse double click or context menu remove the resource from the
    // application
    memberPRsTable.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                processMouseEvent(e);
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                processMouseEvent(e);
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            processMouseEvent(e);
        }

        protected void processMouseEvent(MouseEvent e) {
            int row = memberPRsTable.rowAtPoint(e.getPoint());
            if (row == -1) {
                return;
            }
            if (e.isPopupTrigger()) {
                // context menu
                if (!memberPRsTable.isRowSelected(row)) {
                    // if right click outside the selection then reset selection
                    memberPRsTable.getSelectionModel().setSelectionInterval(row, row);
                }
                JPopupMenu popup = new XJPopupMenu();
                popup.add(removePRAction);
                popup.show(memberPRsTable, e.getPoint().x, e.getPoint().y);
            } else if (e.getID() == MouseEvent.MOUSE_CLICKED) {
                if (e.getClickCount() == 2) {
                    // open the double-clicked PR in the main view.
                    Component root = SwingUtilities.getRoot(SerialControllerEditor.this);
                    if (!(root instanceof MainFrame)) {
                        return;
                    }
                    final MainFrame mainFrame = (MainFrame) root;
                    if (controller != null) {
                        ProcessingResource res = controller.getPRs().get(row);
                        if (res != null)
                            mainFrame.select(res);
                    }
                }
            }
        }
    });
    // Drag and drop support
    memberPRsTable.setTransferHandler(new TransferHandler() {

        // minimal drag and drop that only call the addPRAction when importing
        String source = "";

        @Override
        public int getSourceActions(JComponent c) {
            return MOVE;
        }

        @Override
        protected Transferable createTransferable(JComponent c) {
            int[] selectedRows = memberPRsTable.getSelectedRows();
            Arrays.sort(selectedRows);
            return new StringSelection("memberPRsTable" + Arrays.toString(selectedRows));
        }

        @Override
        protected void exportDone(JComponent c, Transferable data, int action) {
        }

        @Override
        public boolean canImport(JComponent c, DataFlavor[] flavors) {
            for (DataFlavor flavor : flavors) {
                if (DataFlavor.stringFlavor.equals(flavor)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public boolean importData(JComponent c, Transferable t) {
            if (!canImport(c, t.getTransferDataFlavors())) {
                return false;
            }
            try {
                source = (String) t.getTransferData(DataFlavor.stringFlavor);
                if (source.startsWith("memberPRsTable")) {
                    int insertion = memberPRsTable.getSelectedRow();
                    int initialInsertion = insertion;
                    List<ProcessingResource> prs = new ArrayList<ProcessingResource>();
                    source = source.replaceFirst("^memberPRsTable\\[", "");
                    source = source.replaceFirst("\\]$", "");
                    String[] selectedRows = source.split(", ");
                    if (Integer.parseInt(selectedRows[0]) < insertion) {
                        insertion++;
                    }
                    // get the list of PRs selected when dragging started
                    for (String row : selectedRows) {
                        if (Integer.parseInt(row) == initialInsertion) {
                            // the user draged the selected rows on themselves, do nothing
                            return false;
                        }
                        prs.add((ProcessingResource) memberPRsTable.getValueAt(Integer.parseInt(row), memberPRsTable.convertColumnIndexToView(1)));
                        if (Integer.parseInt(row) < initialInsertion) {
                            insertion--;
                        }
                    }
                    // remove the PRs selected when dragging started
                    for (ProcessingResource pr : prs) {
                        controller.remove(pr);
                    }
                    // add the PRs at the insertion point
                    for (ProcessingResource pr : prs) {
                        controller.add(insertion, pr);
                        insertion++;
                    }
                    // select the moved PRs
                    memberPRsTable.addRowSelectionInterval(insertion - selectedRows.length, insertion - 1);
                    return true;
                } else if (source.equals("loadedPRsTable")) {
                    addPRAction.actionPerformed(null);
                    return true;
                } else {
                    return false;
                }
            } catch (UnsupportedFlavorException ufe) {
                return false;
            } catch (IOException ioe) {
                return false;
            }
        }
    });
    loadedPRsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            // disable Add button if no selection
            addButton.setEnabled(loadedPRsTable.getSelectedRowCount() > 0);
        }
    });
    memberPRsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            // disable Remove and Move buttons if no selection
            removeButton.setEnabled(memberPRsTable.getSelectedRowCount() > 0);
            moveUpButton.setEnabled(memberPRsTable.getSelectedRowCount() > 0 && !memberPRsTable.isRowSelected(0));
            moveDownButton.setEnabled(memberPRsTable.getSelectedRowCount() > 0 && !memberPRsTable.isRowSelected(memberPRsTable.getRowCount() - 1));
            // update the parameters & strategy editors
            if (memberPRsTable.getSelectedRowCount() == 1) {
                // only one selection
                selectPR(memberPRsTable.getSelectedRow());
            } else {
                // clean up UI
                selectPR(-1);
            }
        }
    });
    if (conditionalMode) {
        /**
         * A listener called when the selection state changes for any of the
         * execution mode radio buttons. We use selection changes rather than
         * action listeners, as the change of state may not be as results of an
         * action (e.g. editing one of the text fields, changes the selection).
         */
        ItemListener strategyModeListener = new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (selectedPRRunStrategy != null) {
                    if (selectedPRRunStrategy instanceof AnalyserRunningStrategy) {
                        AnalyserRunningStrategy strategy = (AnalyserRunningStrategy) selectedPRRunStrategy;
                        if (yes_RunRBtn.isSelected()) {
                            strategy.setRunMode(RunningStrategy.RUN_ALWAYS);
                        } else if (no_RunRBtn.isSelected()) {
                            strategy.setRunMode(RunningStrategy.RUN_NEVER);
                        } else if (conditional_RunRBtn.isSelected()) {
                            strategy.setRunMode(RunningStrategy.RUN_CONDITIONAL);
                        }
                    } else if (selectedPRRunStrategy instanceof UnconditionalRunningStrategy) {
                        UnconditionalRunningStrategy strategy = (UnconditionalRunningStrategy) selectedPRRunStrategy;
                        if (yes_RunRBtn.isSelected()) {
                            strategy.shouldRun(true);
                        } else if (no_RunRBtn.isSelected()) {
                            strategy.shouldRun(false);
                        }
                    }
                }
                // some icons may have changed!
                memberPRsTable.repaint();
            }
        };
        yes_RunRBtn.addItemListener(strategyModeListener);
        no_RunRBtn.addItemListener(strategyModeListener);
        conditional_RunRBtn.addItemListener(strategyModeListener);
        featureNameTextField.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {

            @Override
            public void insertUpdate(javax.swing.event.DocumentEvent e) {
                changeOccured(e);
            }

            @Override
            public void removeUpdate(javax.swing.event.DocumentEvent e) {
                changeOccured(e);
            }

            @Override
            public void changedUpdate(javax.swing.event.DocumentEvent e) {
                changeOccured(e);
            }

            protected void changeOccured(javax.swing.event.DocumentEvent e) {
                if (selectedPRRunStrategy != null && selectedPRRunStrategy instanceof AnalyserRunningStrategy) {
                    AnalyserRunningStrategy strategy = (AnalyserRunningStrategy) selectedPRRunStrategy;
                    strategy.setFeatureName(featureNameTextField.getText());
                }
                // editing the textfield changes the running strategy to conditional
                conditional_RunRBtn.setSelected(true);
            }
        });
        featureValueTextField.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {

            @Override
            public void insertUpdate(javax.swing.event.DocumentEvent e) {
                changeOccured(e);
            }

            @Override
            public void removeUpdate(javax.swing.event.DocumentEvent e) {
                changeOccured(e);
            }

            @Override
            public void changedUpdate(javax.swing.event.DocumentEvent e) {
                changeOccured(e);
            }

            protected void changeOccured(javax.swing.event.DocumentEvent e) {
                if (selectedPRRunStrategy != null && selectedPRRunStrategy instanceof AnalyserRunningStrategy) {
                    AnalyserRunningStrategy strategy = (AnalyserRunningStrategy) selectedPRRunStrategy;
                    strategy.setFeatureValue(featureValueTextField.getText());
                }
                // editing the textfield changes the running strategy to conditional
                conditional_RunRBtn.setSelected(true);
            }
        });
    }
    if (corpusControllerMode) {
        corpusCombo.addPopupMenuListener(new PopupMenuListener() {

            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                corpusComboModel.fireDataChanged();
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
            }
        });
    }
    addAncestorListener(new AncestorListener() {

        @Override
        public void ancestorAdded(AncestorEvent event) {
            // every time the user switches back to this view, we check
            // whether another controller has just included this one
            loadedPRsTableModel.fireTableDataChanged();
            memberPRsTableModel.fireTableDataChanged();
        }

        @Override
        public void ancestorRemoved(AncestorEvent event) {
        /* do nothing */
        }

        @Override
        public void ancestorMoved(AncestorEvent event) {
        /* do nothing */
        }
    });
    // binds F3 key to the run action
    getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("F3"), "Run");
    getActionMap().put("Run", runAction);
}
Also used : ItemEvent(java.awt.event.ItemEvent) ActionEvent(java.awt.event.ActionEvent) PopupMenuListener(javax.swing.event.PopupMenuListener) ArrayList(java.util.ArrayList) ProcessingResource(gate.ProcessingResource) ListSelectionEvent(javax.swing.event.ListSelectionEvent) PopupMenuEvent(javax.swing.event.PopupMenuEvent) AncestorEvent(javax.swing.event.AncestorEvent) StringSelection(java.awt.datatransfer.StringSelection) DataFlavor(java.awt.datatransfer.DataFlavor) XJPopupMenu(gate.swing.XJPopupMenu) AncestorListener(javax.swing.event.AncestorListener) List(java.util.List) ArrayList(java.util.ArrayList) Component(java.awt.Component) JComponent(javax.swing.JComponent) MouseEvent(java.awt.event.MouseEvent) RunningStrategy(gate.creole.RunningStrategy) AnalyserRunningStrategy(gate.creole.AnalyserRunningStrategy) UnconditionalRunningStrategy(gate.creole.RunningStrategy.UnconditionalRunningStrategy) MouseAdapter(java.awt.event.MouseAdapter) JComponent(javax.swing.JComponent) Transferable(java.awt.datatransfer.Transferable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) XJPopupMenu(gate.swing.XJPopupMenu) JPopupMenu(javax.swing.JPopupMenu) ListSelectionListener(javax.swing.event.ListSelectionListener) ActionListener(java.awt.event.ActionListener) TransferHandler(javax.swing.TransferHandler) ItemListener(java.awt.event.ItemListener) AnalyserRunningStrategy(gate.creole.AnalyserRunningStrategy) UnconditionalRunningStrategy(gate.creole.RunningStrategy.UnconditionalRunningStrategy)

Example 2 with UnconditionalRunningStrategy

use of gate.creole.RunningStrategy.UnconditionalRunningStrategy in project gate-core by GateNLP.

the class ConditionalSerialController method duplicate.

/**
 * Custom duplication method for conditional controllers to handle
 * duplicating the running strategies.
 */
@Override
public Resource duplicate(Factory.DuplicationContext ctx) throws ResourceInstantiationException {
    ConditionalController c = (ConditionalController) super.duplicate(ctx);
    Collection<ProcessingResource> newPRs = c.getPRs();
    List<RunningStrategy> newStrategies = new ArrayList<RunningStrategy>(strategiesList.size());
    Iterator<RunningStrategy> oldRSIt = getRunningStrategies().iterator();
    Iterator<ProcessingResource> prIt = newPRs.iterator();
    while (oldRSIt.hasNext()) {
        RunningStrategy oldStrat = oldRSIt.next();
        ProcessingResource currentPR = prIt.next();
        if (oldStrat instanceof AnalyserRunningStrategy) {
            newStrategies.add(new AnalyserRunningStrategy((LanguageAnalyser) currentPR, ((AnalyserRunningStrategy) oldStrat).getRunMode(), ((AnalyserRunningStrategy) oldStrat).getFeatureName(), ((AnalyserRunningStrategy) oldStrat).getFeatureValue()));
        } else {
            boolean run = true;
            if (oldStrat instanceof UnconditionalRunningStrategy) {
                run = oldStrat.shouldRun();
            }
            // assume an unconditional strategy.  Subclasses that know about other types
            // of strategies can fix this up later
            newStrategies.add(new RunningStrategy.UnconditionalRunningStrategy(currentPR, run));
        }
    }
    c.setRunningStrategies(newStrategies);
    return c;
}
Also used : UnconditionalRunningStrategy(gate.creole.RunningStrategy.UnconditionalRunningStrategy) UnconditionalRunningStrategy(gate.creole.RunningStrategy.UnconditionalRunningStrategy) ProcessingResource(gate.ProcessingResource) ArrayList(java.util.ArrayList) LanguageAnalyser(gate.LanguageAnalyser) UnconditionalRunningStrategy(gate.creole.RunningStrategy.UnconditionalRunningStrategy)

Example 3 with UnconditionalRunningStrategy

use of gate.creole.RunningStrategy.UnconditionalRunningStrategy in project gate-core by GateNLP.

the class UnconditionalRunningStrategyPersistence method extractDataFromSource.

@Override
public void extractDataFromSource(Object source) throws PersistenceException {
    if (!(source instanceof UnconditionalRunningStrategy))
        throw new UnsupportedOperationException(getClass().getName() + " can only be used for " + UnconditionalRunningStrategy.class.getName() + " objects!\n" + source.getClass().getName() + " is not a " + UnconditionalRunningStrategy.class.getName());
    UnconditionalRunningStrategy strategy = (UnconditionalRunningStrategy) source;
    this.pr = PersistenceManager.getPersistentRepresentation(strategy.getPR());
    this.shouldRun = strategy.shouldRun();
}
Also used : UnconditionalRunningStrategy(gate.creole.RunningStrategy.UnconditionalRunningStrategy)

Aggregations

UnconditionalRunningStrategy (gate.creole.RunningStrategy.UnconditionalRunningStrategy)3 ProcessingResource (gate.ProcessingResource)2 ArrayList (java.util.ArrayList)2 LanguageAnalyser (gate.LanguageAnalyser)1 AnalyserRunningStrategy (gate.creole.AnalyserRunningStrategy)1 RunningStrategy (gate.creole.RunningStrategy)1 XJPopupMenu (gate.swing.XJPopupMenu)1 Component (java.awt.Component)1 DataFlavor (java.awt.datatransfer.DataFlavor)1 StringSelection (java.awt.datatransfer.StringSelection)1 Transferable (java.awt.datatransfer.Transferable)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 IOException (java.io.IOException)1 List (java.util.List)1