use of gate.creole.RunningStrategy 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);
}
use of gate.creole.RunningStrategy in project gate-core by GateNLP.
the class ConditionalControllerPersistence method extractDataFromSource.
/**
* Populates this Persistence with the data that needs to be stored from the
* original source object.
*/
@Override
public void extractDataFromSource(Object source) throws PersistenceException {
if (!(source instanceof ConditionalController)) {
throw new UnsupportedOperationException(getClass().getName() + " can only be used for " + ConditionalController.class.getName() + " objects!\n" + source.getClass().getName() + " is not a " + ConditionalController.class.getName());
}
super.extractDataFromSource(source);
ConditionalController controller = (ConditionalController) source;
/*strategiesList = new ArrayList<RunningStrategy>(controller.getRunningStrategies().size());
Iterator<RunningStrategy> stratIter = controller.getRunningStrategies().iterator();
while(stratIter.hasNext()) ((List)strategiesList).add(stratIter.next());*/
strategiesList = new ArrayList<RunningStrategy>(controller.getRunningStrategies());
strategiesList = PersistenceManager.getPersistentRepresentation(strategiesList);
}
use of gate.creole.RunningStrategy in project gate-core by GateNLP.
the class ConditionalControllerPersistence method createObject.
/**
* Creates a new object from the data contained. This new object is supposed
* to be a copy for the original object used as source for data extraction.
*/
@SuppressWarnings("unchecked")
@Override
public Object createObject() throws PersistenceException, ResourceInstantiationException {
ConditionalController controller = (ConditionalController) super.createObject();
controller.setRunningStrategies((Collection<RunningStrategy>) PersistenceManager.getTransientRepresentation(strategiesList, resourceName, initParamOverrides));
return controller;
}
use of gate.creole.RunningStrategy in project gate-core by GateNLP.
the class SerialControllerEditor method selectPR.
/**
* Called when a PR has been selected in the member PRs table;
* @param index row index in {@link #memberPRsTable} (or -1 if no PR is
* currently selected)
*/
protected void selectPR(int index) {
// stop current editing of parameters
if (parametersEditor.getResource() != null) {
try {
parametersEditor.setParameters();
} catch (ResourceInstantiationException rie) {
JOptionPane.showMessageDialog(SerialControllerEditor.this, "Failed to set parameters for \"" + parametersEditor.getResource().getName() + "\"!\n", "GATE", JOptionPane.ERROR_MESSAGE);
rie.printStackTrace(Err.getPrintWriter());
}
if (conditionalMode) {
if (selectedPRRunStrategy != null && selectedPRRunStrategy instanceof AnalyserRunningStrategy) {
AnalyserRunningStrategy strategy = (AnalyserRunningStrategy) selectedPRRunStrategy;
strategy.setFeatureName(featureNameTextField.getText());
strategy.setFeatureValue(featureValueTextField.getText());
}
selectedPRRunStrategy = null;
}
}
// find the new PR
ProcessingResource pr = null;
if (index >= 0 && index < controller.getPRs().size()) {
pr = controller.getPRs().get(index);
}
if (pr != null) {
// update the GUI for the new PR
ResourceData rData = Gate.getCreoleRegister().get(pr.getClass().getName());
// update the border name
parametersBorder.setTitle(" Runtime Parameters for the \"" + pr.getName() + "\" " + rData.getName() + ": ");
// update the params editor
// this is a list of lists
List<List<Parameter>> parameters = rData.getParameterList().getRuntimeParameters();
if (corpusControllerMode) {
// remove corpus and document
// create a new list so we don't change the one from CreoleReg.
List<List<Parameter>> oldParameters = parameters;
parameters = new ArrayList<List<Parameter>>();
for (List<Parameter> aDisjunction : oldParameters) {
List<Parameter> newDisjunction = new ArrayList<Parameter>();
for (Parameter parameter : aDisjunction) {
if (!parameter.getName().equals("corpus") && !parameter.getName().equals("document")) {
newDisjunction.add(parameter);
}
}
if (!newDisjunction.isEmpty())
parameters.add(newDisjunction);
}
}
parametersEditor.init(pr, parameters);
if (conditionalMode) {
strategyBorder.setTitle(" Run \"" + pr.getName() + "\"? ");
// update the state of the run strategy buttons
yes_RunRBtn.setEnabled(true);
no_RunRBtn.setEnabled(true);
conditional_RunRBtn.setEnabled(true);
// editing the strategy panel causes the edits to be sent to the current
// strategy object, which can lead to a race condition.
// So we used a cached value instead.
selectedPRRunStrategy = null;
RunningStrategy newStrategy = ((ConditionalController) controller).getRunningStrategies().get(index);
if (newStrategy instanceof AnalyserRunningStrategy) {
featureNameTextField.setEnabled(true);
featureValueTextField.setEnabled(true);
conditional_RunRBtn.setEnabled(true);
featureNameTextField.setText(((AnalyserRunningStrategy) newStrategy).getFeatureName());
featureValueTextField.setText(((AnalyserRunningStrategy) newStrategy).getFeatureValue());
} else {
featureNameTextField.setEnabled(false);
featureValueTextField.setEnabled(false);
conditional_RunRBtn.setEnabled(false);
}
switch(newStrategy.getRunMode()) {
case RunningStrategy.RUN_ALWAYS:
{
yes_RunRBtn.setSelected(true);
break;
}
case RunningStrategy.RUN_NEVER:
{
no_RunRBtn.setSelected(true);
break;
}
case RunningStrategy.RUN_CONDITIONAL:
{
conditional_RunRBtn.setSelected(true);
break;
}
}
// switch
// now that the UI is updated, we can safely link to the new strategy
selectedPRRunStrategy = newStrategy;
}
} else {
// selected PR == null -> clean all mentions of the old PR from the GUI
parametersBorder.setTitle(" No processing resource selected... ");
parametersEditor.init(null, null);
if (conditionalMode) {
strategyBorder.setTitle(" No processing resource selected... ");
yes_RunRBtn.setEnabled(false);
no_RunRBtn.setEnabled(false);
conditional_RunRBtn.setEnabled(false);
featureNameTextField.setText("");
featureNameTextField.setEnabled(false);
featureValueTextField.setText("");
featureValueTextField.setEnabled(false);
}
}
// this seems to be needed to show the changes
SerialControllerEditor.this.repaint(100);
}
Aggregations