Skip to content

Commit

Permalink
Used Optional instead of returning null for retrieving menus
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhorridge committed Jun 17, 2016
1 parent daf594d commit 3bfc785
Showing 1 changed file with 49 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.protege.editor.owl.ProtegeOWL;
import org.protege.editor.owl.model.entity.OWLEntityCreationSet;
import org.protege.editor.owl.model.event.EventType;
import org.protege.editor.owl.model.event.OWLModelManagerChangeEvent;
import org.protege.editor.owl.model.event.OWLModelManagerListener;
import org.protege.editor.owl.model.inference.*;
import org.protege.editor.owl.model.selection.OWLSelectionHistoryManager;
Expand Down Expand Up @@ -52,7 +51,6 @@
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.net.MalformedURLException;
Expand All @@ -67,7 +65,7 @@
* The University Of Manchester<br>
* Medical Informatics Group<br>
* Date: Mar 17, 2006<br><br>
* <p/>
* matthew.horridge@cs.man.ac.uk<br>
* www.cs.man.ac.uk/~horridgm<br><br>
*/
Expand Down Expand Up @@ -116,7 +114,7 @@ public class OWLWorkspace extends TabbedWorkspace implements SendErrorReportHand
private PrecomputeAction startReasonerAction = new PrecomputeAction();

private PrecomputeAction synchronizeReasonerAction = new PrecomputeAction();

private ProtegeOWLAction stopReasonerAction = new StopReasonerAction();

private ExplainInconsistentOntologyAction explainInconsistentOntologyAction = new ExplainInconsistentOntologyAction();
Expand All @@ -129,10 +127,11 @@ public class OWLWorkspace extends TabbedWorkspace implements SendErrorReportHand
public static final String REASONER_INITIALIZE = "Start reasoner";

public static final String REASONER_RESYNC = "Synchronize reasoner";
public static final String REASONER_STOP = "Stop reasoner";

public static final String REASONER_STOP = "Stop reasoner";

public static final String REASONER_EXPLAIN = "Explain inconsistent ontology";

private ErrorLogListener errorLogListener;

public OWLEditorKit getOWLEditorKit() {
Expand Down Expand Up @@ -170,11 +169,9 @@ public void errorLogCleared() {
ProtegeApplication.getLogManager().addErrorLogListener(errorLogListener);



backgroundTaskLabel = new BackgroundTaskLabel(ProtegeApplication.getBackgroundTaskManager());



createActiveOntologyPanel();
reselectionEventTypes.add(EventType.ACTIVE_ONTOLOGY_CHANGED);
reselectionEventTypes.add(EventType.ONTOLOGY_RELOADED);
Expand All @@ -193,8 +190,7 @@ public void errorLogCleared() {
owlModelManagerListener = event -> {
try {
handleModelManagerEvent(event.getType());
}
catch (Exception t) {
} catch (Exception t) {
logger.warn("An error occurred whilst handling a Model Manager Event: {}", t);
}
};
Expand Down Expand Up @@ -256,8 +252,8 @@ private void updateDirtyFlag() {
JFrame frame = workspaceManager.getFrame(this);
Set<OWLOntology> dirtyOntologies = getOWLModelManager().getDirtyOntologies();
boolean dirty = false;
for(OWLOntology ont : getOWLModelManager().getOntologies()) {
if(dirtyOntologies.contains(ont)) {
for (OWLOntology ont : getOWLModelManager().getOntologies()) {
if (dirtyOntologies.contains(ont)) {
dirty = true;
break;
}
Expand Down Expand Up @@ -349,7 +345,7 @@ private void verifySelection(Set<? extends OWLEntity> entities) {
break;
}
}
if(unreferenced) {
if (unreferenced) {
unreferencedEntities.add(entity);
}
}
Expand Down Expand Up @@ -440,20 +436,22 @@ protected void initialiseExtraMenuItems(JMenuBar menuBar) {
addReasonerListener(menuBar);
updateTitleBar();

JMenu windowMenu = getWindowMenu(menuBar);
if (windowMenu != null) {
windowMenu.addSeparator();
windowMenu.add(new AbstractAction("Refresh user interface") {


public void actionPerformed(ActionEvent e) {
refreshComponents();
}
});
Optional<JMenu> menu = getWindowMenu(menuBar);
if (!menu.isPresent()) {
return;
}
JMenu windowMenu = menu.get();
windowMenu.addSeparator();
windowMenu.add(new AbstractAction("Refresh user interface") {
public void actionPerformed(ActionEvent e) {
refreshComponents();
}
});

}

private class ExtraReasonerMenuActionPlugin extends ProtegeActionPluginJPFImpl {

protected ExtraReasonerMenuActionPlugin(EditorKit editorKit, IExtension extension) {
super(editorKit, extension);
}
Expand Down Expand Up @@ -507,8 +505,8 @@ private List<ProtegeAction> initialiseExtraReasonerMenuActions() {
action.putValue(Action.SHORT_DESCRIPTION, toolTipText);
}
String accelerator = extraReasonerMenuActionPlugin.getAccelerator();
if(accelerator != null) {
action.putValue(Action.ACCELERATOR_KEY,accelerator);
if (accelerator != null) {
action.putValue(Action.ACCELERATOR_KEY, accelerator);
}
result.add(action);
logger.debug("action = " + action);
Expand All @@ -524,12 +522,14 @@ private List<ProtegeAction> initialiseExtraReasonerMenuActions() {
private void rebuildReasonerMenu(JMenuBar menuBar) {
final OWLModelManager mngr = getOWLModelManager();

JMenu reasonerMenu = getReasonerMenu(menuBar);
Optional<JMenu> menu = getReasonerMenu(menuBar);

if(reasonerMenu == null) {
if (!menu.isPresent()) {
return;
}

JMenu reasonerMenu = menu.get();

reasonerMenu.removeAll();

startReasonerAction.setEditorKit(getOWLEditorKit());
Expand All @@ -539,7 +539,7 @@ private void rebuildReasonerMenu(JMenuBar menuBar) {
synchronizeReasonerAction.setEditorKit(getOWLEditorKit());
synchronizeReasonerAction.putValue(Action.NAME, REASONER_RESYNC);
reasonerMenu.add(synchronizeReasonerAction);

stopReasonerAction.setEditorKit(getOWLEditorKit());
stopReasonerAction.putValue(Action.NAME, REASONER_STOP);
reasonerMenu.add(stopReasonerAction);
Expand All @@ -554,12 +554,12 @@ private void rebuildReasonerMenu(JMenuBar menuBar) {
configureAction.putValue(Action.NAME, "Configure...");
reasonerMenu.add(configureAction);

if (extraReasonerMenuActions != null && extraReasonerMenuActions.size() > 0) {
reasonerMenu.addSeparator();
for (ProtegeAction action : extraReasonerMenuActions) {
reasonerMenu.add(action);
}
}
if (extraReasonerMenuActions != null && extraReasonerMenuActions.size() > 0) {
reasonerMenu.addSeparator();
for (ProtegeAction action : extraReasonerMenuActions) {
reasonerMenu.add(action);
}
}

reasonerMenu.addSeparator();

Expand Down Expand Up @@ -608,36 +608,35 @@ public void removed(IExtensionPoint[] extensionPoints) {

}

private JMenu getOntologiesMenu(JMenuBar menuBar) {
private Optional<JMenu> getOntologiesMenu(JMenuBar menuBar) {
return getMenu(menuBar, "Ontologies");
}


private static JMenu getReasonerMenu(JMenuBar menuBar) {
private static Optional<JMenu> getReasonerMenu(JMenuBar menuBar) {
return getMenu(menuBar, "Reasoner");
}


private static JMenu getWindowMenu(JMenuBar menuBar) {
private static Optional<JMenu> getWindowMenu(JMenuBar menuBar) {
return getMenu(menuBar, "Window");
}

private static JMenu getMenu(JMenuBar menuBar, String name) {
private static Optional<JMenu> getMenu(JMenuBar menuBar, String name) {
for (int i = 0; i < menuBar.getMenuCount(); i++) {
JMenu menu = menuBar.getMenu(i);
if (menu != null) {
if (menu.getText() != null) {
if (menu.getText().equals(name)) {
return menu;
return Optional.of(menu);
}
}
}
}
return null;
return Optional.absent();
}



private void createActiveOntologyPanel() {

JPanel topBarPanel = new JPanel(new GridBagLayout());
Expand Down Expand Up @@ -712,7 +711,7 @@ public void actionPerformed(ActionEvent e) {
new Insets(0, 0, 0, 0),
0, 0
)
);
);

add(topBarPanel, BorderLayout.NORTH);

Expand All @@ -734,7 +733,7 @@ public void actionPerformed(ActionEvent e) {
}

public void showSearchDialog() {
if(searchDialog == null) {
if (searchDialog == null) {
searchDialog = SearchDialogPanel.createDialog(this, getOWLEditorKit());
Point workspaceLocation = getLocation();
Dimension workspaceSize = getSize();
Expand Down Expand Up @@ -796,8 +795,7 @@ private void updateReasonerStatus(boolean changesInProgress) {
ReasonerStatus newStatus;
try {
newStatus = reasonerManager.getReasonerStatus();
}
catch (ReasonerDiedException reasonerDied) {
} catch (ReasonerDiedException reasonerDied) {
newStatus = ReasonerStatus.REASONER_NOT_INITIALIZED;
ReasonerUtilities.warnThatReasonerDied(null, reasonerDied);
}
Expand All @@ -809,15 +807,15 @@ private void updateReasonerStatus(boolean changesInProgress) {

private void updateReasonerStatus(ReasonerStatus status) {
reasonerStatus.setText(status.getDescription());

startReasonerAction.setEnabled(status.isEnableInitialization());
startReasonerAction.putValue(Action.SHORT_DESCRIPTION, status.getInitializationTooltip());

synchronizeReasonerAction.setEnabled(status.isEnableSynchronization());
synchronizeReasonerAction.putValue(Action.SHORT_DESCRIPTION, status.getSynchronizationTooltip());

stopReasonerAction.setEnabled(status.isEnableStop());

explainInconsistentOntologyAction.setEnabled(status == ReasonerStatus.INCONSISTENT);

KeyStroke shortcut = KeyStroke.getKeyStroke(KeyEvent.VK_R, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
Expand Down Expand Up @@ -897,8 +895,7 @@ private void rebuildOntologyDropDown() {
ts.addAll(getOWLModelManager().getOntologies());
ontologiesList.setModel(new DefaultComboBoxModel<>(ts.toArray(new OWLOntology[ts.size()])));
ontologiesList.setSelectedItem(getOWLModelManager().getActiveOntology());
}
catch (Exception e) {
} catch (Exception e) {
logger.error("An error occurred whilst building the ontology list: {}", e);
}
}
Expand Down Expand Up @@ -1033,8 +1030,7 @@ public String getIndex() {
public URL getDefaultViewConfigFile() {
try {
return new File(getId() + "-config.xml").toURI().toURL();
}
catch (MalformedURLException uriex) {
} catch (MalformedURLException uriex) {
logger.warn("The default view configuration file is malformed: " + uriex.getMessage());
}
return null;
Expand Down

0 comments on commit 3bfc785

Please sign in to comment.