use of gate.ProcessingResource in project gate-core by GateNLP.
the class SerialAnalyserController method getOffendingPocessingResources.
/**
* Checks whether all the contained PRs have all the required runtime
* parameters set. Ignores the corpus and document parameters as these will be
* set at run time.
*
* @return a {@link List} of {@link ProcessingResource}s that have required
* parameters with null values if they exist <tt>null</tt>
* otherwise.
* @throws {@link ResourceInstantiationException}
* if problems occur while inspecting the parameters for one of the
* resources. These will normally be introspection problems and are
* usually caused by the lack of a parameter or of the read accessor
* for a parameter.
*/
@Override
public List<ProcessingResource> getOffendingPocessingResources() throws ResourceInstantiationException {
// take all the contained PRs
List<ProcessingResource> badPRs = new ArrayList<ProcessingResource>(getPRs());
// remove the ones that no parameters problems
Iterator<ProcessingResource> prIter = getPRs().iterator();
while (prIter.hasNext()) {
ProcessingResource pr = prIter.next();
ResourceData rData = Gate.getCreoleRegister().get(pr.getClass().getName());
// this is a list of lists
List<List<Parameter>> parameters = rData.getParameterList().getRuntimeParameters();
// remove corpus and document
List<List<Parameter>> newParameters = new ArrayList<List<Parameter>>();
Iterator<List<Parameter>> pDisjIter = parameters.iterator();
while (pDisjIter.hasNext()) {
List<Parameter> aDisjunction = pDisjIter.next();
List<Parameter> newDisjunction = new ArrayList<Parameter>(aDisjunction);
Iterator<Parameter> internalParIter = newDisjunction.iterator();
while (internalParIter.hasNext()) {
Parameter parameter = internalParIter.next();
if (parameter.getName().equals("corpus") || parameter.getName().equals("document"))
internalParIter.remove();
}
if (!newDisjunction.isEmpty())
newParameters.add(newDisjunction);
}
if (AbstractResource.checkParameterValues(pr, newParameters)) {
badPRs.remove(pr);
}
}
return badPRs.isEmpty() ? null : badPRs;
}
use of gate.ProcessingResource in project gate-core by GateNLP.
the class MainFrame method resourceLoaded.
@Override
public void resourceLoaded(CreoleEvent e) {
final Resource res = e.getResource();
if (Gate.getHiddenAttribute(res.getFeatures()) || res instanceof VisualResource)
return;
// SwingUtilities.invokeLater(new Runnable() {
// @Override
// public void run() {
NameBearerHandle handle = null;
if (res instanceof Controller) {
handle = new NameBearerHandle(res, MainFrame.this);
DefaultMutableTreeNode node = new DefaultMutableTreeNode(handle, false);
resourcesTreeModel.insertNodeInto(node, applicationsRoot, 0);
} else if (res instanceof ProcessingResource) {
handle = new NameBearerHandle(res, MainFrame.this);
DefaultMutableTreeNode node = new DefaultMutableTreeNode(handle, false);
resourcesTreeModel.insertNodeInto(node, processingResourcesRoot, 0);
} else if (res instanceof LanguageResource) {
handle = new NameBearerHandle(res, MainFrame.this);
DefaultMutableTreeNode node = new DefaultMutableTreeNode(handle, false);
resourcesTreeModel.insertNodeInto(node, languageResourcesRoot, 0);
}
if (handle != null)
handle.addProgressListener(MainFrame.this);
if (handle != null)
handle.addStatusListener(MainFrame.this);
// }
// });
// JPopupMenu popup = handle.getPopup();
//
// // Create a CloseViewAction and a menu item based on it
// CloseViewAction cva = new CloseViewAction(handle);
// XJMenuItem menuItem = new XJMenuItem(cva, this);
// // Add an accelerator ATL+F4 for this action
// menuItem.setAccelerator(KeyStroke.getKeyStroke(
// KeyEvent.VK_H, ActionEvent.CTRL_MASK));
// popup.insert(menuItem, 1);
// popup.insert(new JPopupMenu.Separator(), 2);
//
// popup.insert(new XJMenuItem(
// new RenameResourceAction(
// new TreePath(resourcesTreeModel.getPathToRoot(node))),
// MainFrame.this) , 3);
//
// // Put the action command in the component's action map
// if (handle.getLargeView() != null)
// handle.getLargeView().getActionMap().put("Hide current
// view",cva);
//
}
use of gate.ProcessingResource in project gate-core by GateNLP.
the class MainFrame method resourceUnloaded.
// resourceLoaded();
@Override
public void resourceUnloaded(CreoleEvent e) {
final Resource res = e.getResource();
if (Gate.getHiddenAttribute(res.getFeatures()))
return;
Runnable runner = new Runnable() {
@Override
public void run() {
DefaultMutableTreeNode node;
DefaultMutableTreeNode parent = null;
if (res instanceof Controller) {
parent = applicationsRoot;
} else if (res instanceof ProcessingResource) {
parent = processingResourcesRoot;
} else if (res instanceof LanguageResource) {
parent = languageResourcesRoot;
}
if (parent != null) {
Enumeration<?> children = parent.children();
while (children.hasMoreElements()) {
node = (DefaultMutableTreeNode) children.nextElement();
if (((NameBearerHandle) node.getUserObject()).getTarget() == res) {
resourcesTreeModel.removeNodeFromParent(node);
Handle handle = (Handle) node.getUserObject();
if (handle.viewsBuilt()) {
if (mainTabbedPane.indexOfComponent(handle.getLargeView()) != -1)
mainTabbedPane.remove(handle.getLargeView());
if (lowerScroll.getViewport().getView() == handle.getSmallView())
lowerScroll.getViewport().setView(null);
}
handle.cleanup();
return;
}
}
}
}
};
SwingUtilities.invokeLater(runner);
}
use of gate.ProcessingResource 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.ProcessingResource in project gate-core by GateNLP.
the class NameBearerHandle method buildStaticPopupItems.
protected void buildStaticPopupItems() {
// build the static part of the popup
staticPopupItems = new ArrayList<JComponent>();
if (target instanceof ProcessingResource && !(target instanceof Controller)) {
// actions for PRs (but not Controllers)
staticPopupItems.add(null);
staticPopupItems.add(new XJMenuItem(new ReloadAction(), sListenerProxy));
staticPopupItems.add(new XJMenuItem(new ApplicationWithPRAction(), sListenerProxy));
} else if (target instanceof LanguageResource) {
// Language Resources
staticPopupItems.add(null);
if (target instanceof Document) {
staticPopupItems.add(new XJMenuItem(new CreateCorpusForDocAction(), sListenerProxy));
}
if (target instanceof gate.TextualDocument) {
staticPopupItems.add(null);
staticPopupItems.add(new DocumentExportMenu(this));
} else if (target instanceof Corpus) {
corpusFiller = new CorpusFillerComponent();
scfInputDialog = new SingleConcatenatedFileInputDialog();
staticPopupItems.add(new XJMenuItem(new PopulateCorpusAction(), sListenerProxy));
staticPopupItems.add(new XJMenuItem(new PopulateCorpusFromSingleConcatenatedFileAction(), sListenerProxy));
staticPopupItems.add(null);
staticPopupItems.add(new DocumentExportMenu(this));
}
if (((LanguageResource) target).getDataStore() != null) {
// this item can be used only if the resource belongs to a
// datastore
staticPopupItems.add(new XJMenuItem(new SaveAction(), sListenerProxy));
}
if (!(target instanceof AnnotationSchema)) {
staticPopupItems.add(new XJMenuItem(new SaveToAction(), sListenerProxy));
}
}
if (target instanceof Controller) {
// Applications
staticPopupItems.add(null);
if (target instanceof SerialAnalyserController) {
staticPopupItems.add(new XJMenuItem(new MakeConditionalAction(), sListenerProxy));
}
staticPopupItems.add(new XJMenuItem(new DumpToFileAction(), sListenerProxy));
staticPopupItems.add(new XJMenuItem(new ExportApplicationAction(), sListenerProxy));
}
}
Aggregations