Skip to content

Commit

Permalink
Merge pull request #1469 from nfi/fix-build-warnings
Browse files Browse the repository at this point in the history
Fix unchecked build warnings
  • Loading branch information
nfi authored Jan 30, 2025
2 parents 6caa25d + 619eba0 commit 6d725f3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 60 deletions.
36 changes: 19 additions & 17 deletions java/org/contikios/cooja/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void endDraggingFrame(JComponent f) {
}
});
// Dragging windows on OS X leaves residue from the borders with FlatLaf, so avoid setting dragMode.
if (cooja.configuration.lookAndFeel() != LookAndFeel.FlatLaf) {
if (Cooja.configuration.lookAndFeel() != LookAndFeel.FlatLaf) {
myDesktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
}
frame = new JFrame(WINDOW_TITLE);
Expand Down Expand Up @@ -806,7 +806,7 @@ public void menuSelected(MenuEvent e) {
String description = Cooja.getDescriptionOf(moteTypeClass);
var menuItem = new JMenuItem(description + "...");
menuItem.setActionCommand("create mote type");
menuItem.putClientProperty("class", moteTypeClass);
menuItem.putClientProperty("moteTypeInfo", new MoteTypeContainer(moteTypeClass));
menuItem.addActionListener(guiEventHandler);

// Add new item directly after cross level separator.
Expand Down Expand Up @@ -910,14 +910,13 @@ public boolean shouldBeEnabled() {
// Tools menu.
toolsMenu.addMenuListener(new MenuListener() {
private final ActionListener menuItemListener = e -> {
Object pluginClass = ((JMenuItem)e.getSource()).getClientProperty("class");
Object mote = ((JMenuItem)e.getSource()).getClientProperty("mote");
cooja.tryStartPlugin((Class<? extends Plugin>) pluginClass, cooja.getSimulation(), (Mote)mote);
var pluginInfo = (PluginInfoContainer)((JMenuItem)e.getSource()).getClientProperty("pluginInfo");
cooja.tryStartPlugin(pluginInfo.pluginType(), cooja.getSimulation(), pluginInfo.mote());
};
private JMenuItem createMenuItem(Class<? extends Plugin> newPluginClass) {
String description = Cooja.getDescriptionOf(newPluginClass);
JMenuItem menuItem = new JMenuItem(description + "...");
menuItem.putClientProperty("class", newPluginClass);
menuItem.putClientProperty("pluginInfo", new PluginInfoContainer(newPluginClass, null));
menuItem.addActionListener(menuItemListener);
// Only enable items when there is a simulation, otherwise the user gets a dialog with a backtrace.
menuItem.setEnabled(cooja.getSimulation() != null);
Expand Down Expand Up @@ -1192,9 +1191,8 @@ public JMenu createMotePluginsSubmenu(Class<? extends Plugin> pluginClass) {
}

ActionListener menuItemListener = e -> {
Object pluginClass1 = ((JMenuItem)e.getSource()).getClientProperty("class");
Object mote = ((JMenuItem)e.getSource()).getClientProperty("mote");
cooja.tryStartPlugin((Class<? extends Plugin>) pluginClass1, cooja.getSimulation(), (Mote)mote);
var pluginInfo = (PluginInfoContainer)((JMenuItem)e.getSource()).getClientProperty("pluginInfo");
cooja.tryStartPlugin(pluginInfo.pluginType(), cooja.getSimulation(), pluginInfo.mote());
};

final int MAX_PER_ROW = 30;
Expand All @@ -1206,8 +1204,7 @@ public JMenu createMotePluginsSubmenu(Class<? extends Plugin> pluginClass) {
continue;
}
JMenuItem menuItem = new JMenuItem(mote.toString() + "...");
menuItem.putClientProperty("class", pluginClass);
menuItem.putClientProperty("mote", mote);
menuItem.putClientProperty("pluginInfo", new PluginInfoContainer(pluginClass, mote));
menuItem.addActionListener(menuItemListener);
menu.add(menuItem);
added++;
Expand All @@ -1231,8 +1228,7 @@ public JMenu createMotePluginsSubmenu(Mote mote) {
continue;
}
var menuItem = new JMenuItem(new StartPluginGUIAction(Cooja.getDescriptionOf(motePluginClass) + "..."));
menuItem.putClientProperty("class", motePluginClass);
menuItem.putClientProperty("mote", mote);
menuItem.putClientProperty("pluginInfo", new PluginInfoContainer(motePluginClass, mote));
menuMotePlugins.add(menuItem);
}
return menuMotePlugins;
Expand Down Expand Up @@ -1823,7 +1819,7 @@ public void actionPerformed(ActionEvent e) {
case "create mote type" -> {
cooja.getSimulation().stopSimulation();
// Create mote type
var clazz = (Class<? extends MoteType>) ((JMenuItem) e.getSource()).getClientProperty("class");
var clazz = ((MoteTypeContainer)((JMenuItem) e.getSource()).getClientProperty("moteTypeInfo")).moteTypeClass();
try {
newMoteType = ExtensionManager.createMoteType(cooja, clazz.getName());
if (newMoteType == null || !newMoteType.configureAndInit(frame, cooja.getSimulation(), true)) {
Expand Down Expand Up @@ -1955,13 +1951,19 @@ class StartPluginGUIAction extends GUIAction {
}
@Override
public void actionPerformed(final ActionEvent e) {
var pluginClass = (Class<Plugin>) ((JMenuItem) e.getSource()).getClientProperty("class");
Mote mote = (Mote) ((JMenuItem) e.getSource()).getClientProperty("mote");
cooja.tryStartPlugin(pluginClass, cooja.getSimulation(), mote);
var pluginInfo = (PluginInfoContainer)((JMenuItem) e.getSource()).getClientProperty("pluginInfo");
cooja.tryStartPlugin(pluginInfo.pluginType(), cooja.getSimulation(), pluginInfo.mote());
}
@Override
public boolean shouldBeEnabled() {
return cooja.getSimulation() != null;
}
}

private record PluginInfoContainer(Class<? extends Plugin> pluginType, Mote mote) {
}

private record MoteTypeContainer(Class<? extends MoteType> moteTypeClass) {
}

}
15 changes: 9 additions & 6 deletions java/org/contikios/cooja/dialogs/AbstractCompileDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,9 @@ public BaseContikiMoteType.MoteTypeConfig results() {
}
ArrayList<Class<? extends MoteInterface>> selected = new ArrayList<>();
for (var c : moteIntfBox.getComponents()) {
if (c instanceof JCheckBox checkbox) {
if (checkbox.isSelected()) {
selected.add((Class<? extends MoteInterface>) checkbox.getClientProperty("class"));
}
if (c instanceof JCheckBox checkbox && checkbox.isSelected()
&& checkbox.getClientProperty("interfaceType") instanceof InterfaceContainer interfaceClass) {
selected.add(interfaceClass.interfaceClass());
}
}
return new BaseContikiMoteType.MoteTypeConfig(descriptionField.getText(), null, contikiField.getText(),
Expand Down Expand Up @@ -507,7 +506,8 @@ protected void addMoteInterface(Class<? extends MoteInterface> intfClass, boolea
if (!(c instanceof JCheckBox checkBox)) {
continue;
}
if (checkBox.getClientProperty("class") == intfClass) {
if (checkBox.getClientProperty("interfaceType") instanceof InterfaceContainer interfaceClass
&& interfaceClass.interfaceClass() == intfClass) {
checkBox.setSelected(selected);
return;
}
Expand All @@ -516,7 +516,7 @@ protected void addMoteInterface(Class<? extends MoteInterface> intfClass, boolea
/* Create new mote interface checkbox */
JCheckBox intfCheckBox = new JCheckBox(Cooja.getDescriptionOf(intfClass));
intfCheckBox.setSelected(selected);
intfCheckBox.putClientProperty("class", intfClass);
intfCheckBox.putClientProperty("interfaceType", new InterfaceContainer(intfClass));
intfCheckBox.setAlignmentX(Component.LEFT_ALIGNMENT);
intfCheckBox.setToolTipText(intfClass.getName());
intfCheckBox.addActionListener(e -> {
Expand Down Expand Up @@ -558,4 +558,7 @@ private void abortAnyCompilation() {
currentCompilationProcess = null;
}

private record InterfaceContainer(Class<? extends MoteInterface> interfaceClass) {
}

}
28 changes: 10 additions & 18 deletions java/org/contikios/cooja/plugins/BufferListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
*/

package org.contikios.cooja.plugins;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.awt.BorderLayout;
Expand All @@ -44,7 +43,6 @@
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
Expand Down Expand Up @@ -496,8 +494,14 @@ public void menuSelected(MenuEvent e) {
bufferMenu.addSeparator();
}
JCheckBoxMenuItem mi = new JCheckBoxMenuItem(Cooja.getDescriptionOf(btClass), btClass == buffer.getClass());
mi.putClientProperty("CLASS", btClass);
mi.addActionListener(bufferSelectedListener);
mi.addActionListener(bufferSelectedEvent -> {
var b = createBufferInstance(btClass);
if (b != null) {
if (b.configure(BufferListener.this)) {
setBuffer(b);
}
}
});
bufferMenu.add(mi);
}
}
Expand All @@ -515,8 +519,7 @@ public void menuSelected(MenuEvent e) {
parserMenu.removeAll();
for (var bpClass: bufferParsers) {
JCheckBoxMenuItem mi = new JCheckBoxMenuItem(Cooja.getDescriptionOf(bpClass), bpClass == parser.getClass());
mi.putClientProperty("CLASS", bpClass);
mi.addActionListener(parserSelectedListener);
mi.addActionListener(parserSelectedEvent -> setParser(bpClass));
parserMenu.add(mi);
}
}
Expand Down Expand Up @@ -1205,18 +1208,6 @@ public void actionPerformed(ActionEvent e) {
}
};

private final ActionListener parserSelectedListener =
e -> setParser((Class<? extends Parser>) ((JMenuItem) e.getSource()).getClientProperty("CLASS"));

private final ActionListener bufferSelectedListener = e -> {
var b = createBufferInstance((Class<? extends Buffer>) ((JMenuItem) e.getSource()).getClientProperty("CLASS"));
if (b != null) {
if (b.configure(BufferListener.this)) {
setBuffer(b);
}
}
};

private void setParser(Class<? extends Parser> bpClass) {
Parser bp;
try {
Expand Down Expand Up @@ -1921,4 +1912,5 @@ public boolean configure(BufferListener bl) {
return true;
}
}

}
27 changes: 8 additions & 19 deletions java/org/contikios/cooja/plugins/Visualizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,43 +269,30 @@ public void menuSelected(MenuEvent e) {
break;
}
}
var item = new JCheckBoxMenuItem(Cooja.getDescriptionOf(skinClass), activated);
item.putClientProperty("skinclass", skinClass);
item.addItemListener(e1 -> {
var menuItem = ((JCheckBoxMenuItem) e1.getItem());
if (menuItem == null) {
logger.error("No menu item");
return;
}

var skinClass1 = (Class<VisualizerSkin>) menuItem.getClientProperty("skinclass");
if (skinClass1 == null) {
logger.error("Unknown visualizer skin class");
return;
}

var menuItem = new JCheckBoxMenuItem(Cooja.getDescriptionOf(skinClass), activated);
menuItem.addItemListener(e1 -> {
if (menuItem.isSelected()) {
// Create and activate new skin.
generateAndActivateSkin(skinClass1);
generateAndActivateSkin(skinClass);
} else {
// Deactivate skin.
VisualizerSkin skinToDeactivate = null;
for (var skin : currentSkins) {
if (skin.getClass() == skinClass1) {
if (skin.getClass() == skinClass) {
skinToDeactivate = skin;
break;
}
}
if (skinToDeactivate == null) {
logger.error("Unknown visualizer to deactivate: " + skinClass1);
logger.error("Unknown visualizer to deactivate: " + skinClass);
return;
}
skinToDeactivate.setInactive();
repaint();
currentSkins.remove(skinToDeactivate);
}
});
viewMenu.add(item);
viewMenu.add(menuItem);
}
}

Expand Down Expand Up @@ -750,6 +737,7 @@ public void drop(DropTargetDropEvent dtde) {
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

try {
@SuppressWarnings("unchecked")
List<File> list = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
if (list.size() != 1) {
return;
Expand Down Expand Up @@ -782,6 +770,7 @@ private boolean acceptOrRejectDrag(DropTargetDragEvent dtde) {
return false;
}
try {
@SuppressWarnings("unchecked")
List<File> list = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
if (list.size() != 1) {
return false;
Expand Down
1 change: 1 addition & 0 deletions java/org/contikios/mrm/ObstacleWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ObstacleWorld {

// All registered obstacles, with spatial information
private static final int spatialResolution = 10;
@SuppressWarnings("unchecked")
private final ArrayList<Rectangle2D>[][] allObstaclesSpatial = new ArrayList[spatialResolution][spatialResolution];
private boolean obstaclesOrganized;

Expand Down

0 comments on commit 6d725f3

Please sign in to comment.