use of ini.trakem2.display.Displayable in project TrakEM2 by trakem2.
the class ProjectThing method measure.
/**
* Call on children things, and on itself if it contains a basic data type directly.
* All children of the same type report to the same table.
* Result tables are returned without ever displaying them.
*/
public HashMap<Class<?>, ResultsTable> measure(HashMap<Class<?>, ResultsTable> ht) {
if (null == ht)
ht = new HashMap<Class<?>, ResultsTable>();
if (null != object && object instanceof Displayable) {
Displayable d = (Displayable) object;
if (d.isVisible()) {
ResultsTable rt = d.measure(ht.get(d.getClass()));
if (null != rt)
ht.put(d.getClass(), rt);
// Areas:
if (object instanceof AreaContainer) {
ResultsTable rta = ((AreaContainer) object).measureAreas(ht.get(AreaContainer.class));
if (null != rta)
ht.put(AreaContainer.class, rta);
}
} else {
Utils.log("Measure: skipping hidden object " + d.getProject().getMeaningfulTitle(d));
}
}
if (null == al_children)
return ht;
// profile list: always special ...
if (template.getType().equals("profile_list") && null != al_children) {
synchronized (al_children) {
if (al_children.size() > 1) {
Profile[] p = new Profile[al_children.size()];
for (int i = 0; i < al_children.size(); i++) p[i] = (Profile) al_children.get(i).object;
ResultsTable rt = Profile.measure(p, ht.get(Profile.class), this.id);
if (null != rt)
ht.put(Profile_List.class, rt);
// return ht; // don't return: do each profile separately as well
}
}
}
synchronized (al_children) {
for (ProjectThing child : al_children) child.measure(ht);
}
return ht;
}
use of ini.trakem2.display.Displayable in project TrakEM2 by trakem2.
the class ProjectTree method mousePressed.
public void mousePressed(final MouseEvent me) {
super.dispatcher.execSwing(new Runnable() {
public void run() {
if (!me.getSource().equals(ProjectTree.this) || !project.isInputEnabled()) {
return;
}
final int x = me.getX();
final int y = me.getY();
// find the node and set it selected
final TreePath path = getPathForLocation(x, y);
if (null == path) {
return;
}
ProjectTree.this.setSelectionPath(path);
selected_node = (DefaultMutableTreeNode) path.getLastPathComponent();
if (2 == me.getClickCount() && !me.isPopupTrigger() && MouseEvent.BUTTON1 == me.getButton()) {
// show in the front Display
if (null == selected_node)
return;
Object obt = selected_node.getUserObject();
if (!(obt instanceof ProjectThing))
return;
ProjectThing thing = (ProjectThing) obt;
thing.setVisible(true);
Object obd = thing.getObject();
if (obd instanceof Displayable) {
// additionaly, get the front Display (or make a new one if none) and show in it the layer in which the Displayable object is contained.
Displayable displ = (Displayable) obd;
Display.showCentered(displ.getLayer(), displ, true, me.isShiftDown());
}
return;
} else if (me.isPopupTrigger() || (ij.IJ.isMacOSX() && me.isControlDown()) || MouseEvent.BUTTON2 == me.getButton() || 0 != (me.getModifiers() & Event.META_MASK)) {
// the last block is from ij.gui.ImageCanvas, aparently to make the right-click work on windows?
JPopupMenu popup = getPopupMenu(selected_node);
if (null == popup)
return;
popup.show(ProjectTree.this, x, y);
return;
}
}
});
}
use of ini.trakem2.display.Displayable in project TrakEM2 by trakem2.
the class ProjectTree method tryAddNewConnector.
/**
* If the parent node of {@code active} can accept a Connector or has a direct child
* node that can accept a {@link Connector}, add a new {@link Connector} and return it.
*
* @param d The {@link Displayable} that serves as reference, to decide which node to add the new {@link Connector}.
* @param selectNode Whether to select the new node containing the {@link Connector} in the ProjectTree.
*
* @return The newly created {@link Connector}.
*/
public Connector tryAddNewConnector(final Displayable d, final boolean selectNode) {
ProjectThing pt = project.findProjectThing(d);
ProjectThing parent = (ProjectThing) pt.getParent();
TemplateThing connectorType = project.getTemplateThing("connector");
boolean add = false;
if (parent.canHaveAsChild(connectorType)) {
// Add as a sibling of pt
add = true;
} else {
// Inspect if any of the sibling nodes can have it as child
for (final ProjectThing child : parent.getChildren()) {
if (child.canHaveAsChild(connectorType)) {
parent = child;
add = true;
break;
}
}
}
Connector c = null;
DefaultMutableTreeNode node = null;
if (add) {
// reuse same String instance
c = new Connector(project, connectorType.getType());
node = addChild(parent, connectorType.getType(), c);
}
if (null != node) {
d.getLayerSet().add(c);
if (selectNode)
setSelectionPath(new TreePath(node.getPath()));
return c;
}
Utils.logAll("Could not add a new Connector related to " + d);
return null;
}
use of ini.trakem2.display.Displayable in project TrakEM2 by trakem2.
the class ProjectTree method keyPressed.
@Override
public void keyPressed(final KeyEvent ke) {
super.keyPressed(ke);
if (ke.isConsumed())
return;
if (!ke.getSource().equals(ProjectTree.this) || !project.isInputEnabled()) {
return;
}
// get the first selected node only
TreePath path = getSelectionPath();
if (null == path)
return;
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
if (null == node)
return;
final ProjectThing pt = (ProjectThing) node.getUserObject();
if (null == pt)
return;
//
final int flags = ke.getModifiers();
switch(ke.getKeyCode()) {
case KeyEvent.VK_PAGE_UP:
move(node, -1);
// in any case
ke.consume();
break;
case KeyEvent.VK_PAGE_DOWN:
move(node, 1);
// in any case
ke.consume();
break;
case KeyEvent.VK_F2:
rename(pt);
ke.consume();
break;
case KeyEvent.VK_H:
if (0 == (flags ^ Event.ALT_MASK)) {
pt.setVisible(true);
ke.consume();
} else if (0 == flags) {
pt.setVisible(false);
ke.consume();
}
break;
case KeyEvent.VK_A:
if (0 == flags || (0 == (flags ^ Event.SHIFT_MASK))) {
selectInDisplay(pt, 0 == (flags ^ Event.SHIFT_MASK));
ke.consume();
}
break;
case KeyEvent.VK_3:
if (0 == flags) {
ini.trakem2.display.Display3D.showAndResetView(pt);
ke.consume();
break;
}
// else, flow:
case KeyEvent.VK_1:
case KeyEvent.VK_2:
case KeyEvent.VK_4:
case KeyEvent.VK_5:
case KeyEvent.VK_6:
case KeyEvent.VK_7:
case KeyEvent.VK_8:
case KeyEvent.VK_9:
// run a plugin, if any
if (pt.getObject() instanceof Displayable && null != Utils.launchTPlugIn(ke, "Project Tree", project, (Displayable) pt.getObject())) {
ke.consume();
}
break;
}
ke.consume();
}
use of ini.trakem2.display.Displayable in project TrakEM2 by trakem2.
the class FilePathRepair method fixPath.
private static void fixPath(final JTable table, final PathTableModel data, final int row, final boolean fix_similar, final boolean fix_all, final boolean update_mipmaps) throws Exception {
synchronized (projects) {
final Patch patch = data.vp.get(row);
if (null == patch)
return;
final String old_path = patch.getImageFilePath();
final File f = new File(old_path);
if (f.exists()) {
Utils.log("File exists for " + patch + " at " + f.getAbsolutePath() + "\n --> not updating path.");
data.remove(row);
return;
}
// Else, pop up file dialog
OpenDialog od = new OpenDialog("Select image file", OpenDialog.getDefaultDirectory(), null);
String dir = od.getDirectory();
final String filename = od.getFileName();
// dialog was canceled
if (null == dir)
return;
if (IJ.isWindows())
dir = dir.replace('\\', '/');
if (!dir.endsWith("/"))
dir += "/";
// Compare filenames
if (!filename.equals(f.getName())) {
YesNoDialog yn = new YesNoDialog(projects.get(patch.getProject()).frame, "WARNING", "Different file names!\n old: " + f.getName() + "\n new: " + filename + "\nSet to new file name?");
if (!yn.yesPressed())
return;
// Remove mipmaps: would not be found with the new name and the old ones would remain behind unused
if (!f.getName().equals(new File(old_path).getName())) {
// remove mipmaps: the name wouldn't match otherwise
patch.getProject().getLoader().removeMipMaps(patch);
}
}
//
String wrong_parent_path = new File(data.vpath.get(row)).getParent();
wrong_parent_path = wrong_parent_path.replace('\\', '/');
// not File.separatorChar, TrakEM2 uses '/' as folder separator
if (!wrong_parent_path.endsWith("/"))
wrong_parent_path = new StringBuilder(wrong_parent_path).append('/').toString();
final String path = new StringBuilder(dir).append(filename).toString();
// keep track of fixed slices to avoid calling n_slices * n_slices times!
final HashSet<Patch> fixed = new HashSet<Patch>();
int n_fixed = 0;
if (-1 == patch.getFilePath().lastIndexOf("-----#slice=")) {
if (!fixPatchPath(patch, path, update_mipmaps)) {
return;
}
data.remove(patch);
fixed.add(patch);
n_fixed += 1;
} else {
int n = fixStack(data, fixed, patch.getStackPatches(), old_path, path, update_mipmaps);
if (0 == n) {
// some error ocurred, no paths fixed
return;
}
n_fixed += n;
// data already cleared of removed patches by fixStack
}
String good_parent_path = dir;
// not File.separatorChar, TrakEM2 uses '/' as folder separator
if (!dir.endsWith("/"))
good_parent_path = new StringBuilder(good_parent_path).append('/').toString();
// Check for similar parent paths and see if they can be fixed
if (fix_similar) {
for (int i = data.vp.size() - 1; i > -1; i--) {
final String wrong_path = data.vpath.get(i);
final Patch p = data.vp.get(i);
if (wrong_path.startsWith(wrong_parent_path)) {
// try to fix as well
final File file = new File(new StringBuilder(good_parent_path).append(wrong_path.substring(wrong_parent_path.length())).toString());
if (file.exists()) {
if (-1 == p.getFilePath().lastIndexOf("-----#slice=")) {
if (!fixed.contains(p) && fixPatchPath(p, file.getAbsolutePath(), update_mipmaps)) {
// not by 'i' but by Patch, since if some fail the order is not the same
data.remove(p);
n_fixed++;
fixed.add(p);
}
} else {
if (fixed.contains(p))
continue;
n_fixed += fixStack(data, fixed, p.getStackPatches(), wrong_path, file.getAbsolutePath(), update_mipmaps);
}
}
}
}
}
if (fix_all) {
// traverse all Patch from the entire project, minus those already fixed
for (final Displayable d : patch.getLayerSet().getDisplayables(Patch.class)) {
final Patch p = (Patch) d;
final String wrong_path = p.getImageFilePath();
if (wrong_path.startsWith(wrong_parent_path)) {
File file = new File(new StringBuilder(good_parent_path).append(wrong_path.substring(wrong_parent_path.length())).toString());
if (file.exists()) {
if (-1 == p.getFilePath().lastIndexOf("-----#slice=")) {
if (!fixed.contains(p) && fixPatchPath(p, file.getAbsolutePath(), update_mipmaps)) {
// not by 'i' but by Patch, since if some fail the order is not the same
data.remove(p);
n_fixed++;
fixed.add(p);
}
} else {
if (fixed.contains(p))
continue;
n_fixed += fixStack(data, fixed, p.getStackPatches(), wrong_path, file.getAbsolutePath(), update_mipmaps);
}
}
}
}
}
// if table is empty, close
if (0 == data.vp.size()) {
FilePathRepair fpr = projects.remove(patch.getProject());
fpr.frame.dispose();
}
Utils.logAll("Fixed " + n_fixed + " image file path" + (n_fixed > 1 ? "s" : ""));
}
}
Aggregations