/** * Switches views based on the passed enum. * * @param v the View to switch to * @param o A BattleField if switching to BattleView. */ public void changeView(Views v, Object o) { switch (v) { case PREVIOUS: CardLayout cl = (CardLayout) body.getLayout(); JPanel temp = currentPanel; panels.pop(); currentPanel = panels.peek(); cl.show(body, currentPanel.toString()); cl.removeLayoutComponent(temp); break; case BATTLE: currentPanel = new BattleView(this, (BattleField) o); panels.push(currentPanel); body.add(currentPanel, v.name()); CardLayout cl1 = (CardLayout) body.getLayout(); cl1.show(body, v.name()); for (Component c : body.getComponents()) { if (c == currentPanel) { c.requestFocusInWindow(); } } break; case TITLE: currentPanel = new TitleView(this); panels.push(currentPanel); body.add(currentPanel, v.name()); CardLayout cl2 = (CardLayout) body.getLayout(); cl2.show(body, v.name()); break; default: // Do nothing } }
/** Focuses the component upon mouseover. */ @Override public void mouseEntered(MouseEvent e) { Object src = e.getSource(); if (src instanceof Component) { ((Component) src).requestFocusInWindow(); } }
/* * Fix for BugTraq ID 4041703. * Set the focus to a reasonable default for an Applet. */ private void setDefaultFocus() { Component toFocus = null; Container parent = getParent(); if (parent != null) { if (parent instanceof Window) { toFocus = getMostRecentFocusOwnerForWindow((Window) parent); if (toFocus == parent || toFocus == null) { toFocus = parent.getFocusTraversalPolicy().getInitialComponent((Window) parent); } } else if (parent.isFocusCycleRoot()) { toFocus = parent.getFocusTraversalPolicy().getDefaultComponent(parent); } } if (toFocus != null) { if (parent instanceof EmbeddedFrame) { ((EmbeddedFrame) parent).synthesizeWindowActivation(true); } // EmbeddedFrame might have focus before the applet was added. // Thus after its activation the most recent focus owner will be // restored. We need the applet's initial focusabled component to // be focused here. toFocus.requestFocusInWindow(); } }
/** Prepare the window that shall show the openGL content. */ private void initWindow() { IllarionLookAndFeel.setupLookAndFeel(); // create canvas that shall show the openGL content display = Graphics.getInstance().getRenderDisplay(); display.getRenderArea().setBackground(Color.red); configChanged(IllaClient.getCfg(), CFG_RESOLUTION); final Component displayParent = display.getRenderArea(); displayParent.setBackground(Color.green); displayParent.setVisible(true); // set up the window settings displayFrame = new Frame(); displayFrame.setLayout(new BorderLayout(0, 0)); displayFrame.setTitle(IllaClient.getVersionText()); displayFrame.setBackground(Color.black); setIcon(displayFrame); displayFrame.addWindowListener(new ClientWindowListener()); displayFrame.setResizable(false); displayFrame.setFocusable(false); displayFrame.setFocusableWindowState(true); displayFrame.setFocusTraversalKeysEnabled(false); displayParent.setFocusable(true); displayParent.setFocusTraversalKeysEnabled(false); // add the canvas to the window and make the canvas the openGL render // target. displayFrame.add(displayParent, BorderLayout.CENTER); displayFrame.pack(); displayFrame.setLocationRelativeTo(null); displayFrame.setVisible(true); displayParent.requestFocusInWindow(); final RenderDisplay usedDisplay = display; Graphics.getInstance() .getRenderManager() .addTask( new RenderTask() { @Override public boolean render(final int delta) { usedDisplay.startRendering(); return false; } }); }
public void requestFocus() { TextArea textArea = getTextArea(); if (textArea != null) textArea.requestFocus(); else if (widget != null) { Component component = findFirstComponent(widget, FOCUS, Constraint.AMONG); if (component != null) { component.requestFocusInWindow(); diag_println( DIAG_OFF, "focus", identity(component), identity(SwingUtilities.getAncestorOfClass(EditPane.class, component))); } } }
@Override public boolean editCellAt(final int row, final int column, final EventObject event) { final boolean editingStarted = super.editCellAt(row, column, event); if (editingStarted) { final CellEditor cellEditor = getCellEditor(); try { final Object o = cellEditor.getClass().getMethod("getComponent").invoke(cellEditor); if (o instanceof Component) { ((Component) o).requestFocusInWindow(); } } catch (final Exception e) { // ignore } } return editingStarted; }
private boolean validateInput() { String error = null; Component errorComponent = null; if (tfFileName.getText().trim().length() < 1) { error = resources.getString("error.fileName.required"); errorComponent = tfFileName; } if (error != null) { GUIUtil.showError(this, error); if (errorComponent != null) { errorComponent.requestFocusInWindow(); } return false; } return true; }
/* * Tab has changed. Focus on saved component for the given tab. * When there is no saved component, focus on the first component. */ public void stateChanged(ChangeEvent e) { Component key = tabbedPane.getComponentAt(tabbedPane.getSelectedIndex()); if (key == null) return; Component value = tabFocus.get(key); // First time selecting this tab or focus policy is RESET_FOCUS if (value == null) { key.transferFocus(); tabFocus.put(key, value); } else // Use the saved component for focusing { value.requestFocusInWindow(); } }
public void setVisible(boolean b) { KeyboardFocusManager keyboardFocusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); if (b) { keyboardFocusManager.addKeyEventDispatcher(keyManager); } else { keyboardFocusManager.removeKeyEventDispatcher(keyManager); } super.setVisible(b); Window owner = getOwner(); if (owner != null) { owner.requestFocus(); if (lastFocusOwner != null) { lastFocusOwner.requestFocusInWindow(); } } }
/** Gives focus to a {@linkplain Component} by its name */ protected static boolean focusByName(final String name) throws ComponentNotFoundException, MultipleComponentsFoundException { Component c = findByName(name); if (c.hasFocus()) return true; // try ordinary method if (c.requestFocusInWindow()) { while (!c.hasFocus()) guiSleep(); return true; } // press tab until we have the correct focus for (int i = 0; i < 25 /* TODO proper number */; i++) { tester.keyStroke('\t'); guiSleep(); if (name.equals(AWT.getActiveWindow().getFocusOwner().getName())) return true; } // failed ... logger.warning("Could not give focus to component: " + name); return true; }
private void moveFocus() { int col = table.getSelectedColumn(); int row = table.getSelectedRow(); // table.nextFocus(); // table.setCellSelectionEnabled(true); int moveToCol = 0; int moveToRow = 0; if (table.getColumnCount() < col) { moveToCol = col + 1; } if (col == 3 && table.getSelectedRow() < row) { moveToRow = row + 1; } table.changeSelection(moveToRow, moveToCol, true, false); table.editCellAt(moveToRow, moveToCol); Component c = table.getEditorComponent(); if (c != null) { c.requestFocusInWindow(); } }
/** * Requests focus unless the component already has focus. For some weird reason calling {@link * Component#requestFocusInWindow()}when the component is focus owner changes focus owner to * another component! * * @param component the component to request focus for * @return true if the component has focus or probably will get focus, otherwise false */ public static boolean requestFocus(Component component) { return KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == component || component.requestFocusInWindow(); }
static void requestFocusInWindow(Object display) { ((Component) display).requestFocusInWindow(); }
@SuppressWarnings("unchecked") private void okButtonActionPerformed(ActionEvent evt) { boolean modified = false; Map<String, Field> fields = Configuration.getConfigurationFields(); Map<String, Object> values = new HashMap<>(); for (String name : fields.keySet()) { Component c = componentsMap.get(name); Object value = null; ParameterizedType listType = (ParameterizedType) fields.get(name).getGenericType(); java.lang.reflect.Type itemType2 = listType.getActualTypeArguments()[0]; if (!(itemType2 instanceof Class<?>)) { continue; } Class itemType = (Class<?>) itemType2; if (name.equals("gui.skin")) { value = ((SkinSelect) ((JComboBox<SkinSelect>) c).getSelectedItem()).className; } else if (itemType == String.class) { value = ((JTextField) c).getText(); } if (itemType == Boolean.class) { value = ((JCheckBox) c).isSelected(); } if (itemType == Calendar.class) { Calendar cal = Calendar.getInstance(); try { cal.setTime(new SimpleDateFormat().parse(((JTextField) c).getText())); } catch (ParseException ex) { c.requestFocusInWindow(); return; } value = cal; } if (itemType.isEnum()) { String stringValue = (String) ((JComboBox<String>) c).getSelectedItem(); value = Enum.valueOf(itemType, stringValue); } try { if (itemType == Integer.class) { value = Integer.parseInt(((JTextField) c).getText()); } if (itemType == Long.class) { value = Long.parseLong(((JTextField) c).getText()); } if (itemType == Double.class) { value = Double.parseDouble(((JTextField) c).getText()); } if (itemType == Float.class) { value = Float.parseFloat(((JTextField) c).getText()); } } catch (NumberFormatException nfe) { if (!((JTextField) c).getText().isEmpty()) { c.requestFocusInWindow(); return; } // else null } values.put(name, value); } for (String name : fields.keySet()) { Component c = componentsMap.get(name); Object value = values.get(name); Field field = fields.get(name); ConfigurationItem item = null; try { item = (ConfigurationItem) field.get(null); } catch (IllegalArgumentException | IllegalAccessException ex) { // Reflection exceptions. This should never happen throw new Error(ex.getMessage()); } if (item.get() == null || !item.get().equals(value)) { if (item.hasValue() || value != null) { item.set(value); modified = true; } } } Configuration.saveConfig(); setVisible(false); if (modified) { showRestartConfirmDialod(); } }
/** {@inheritDoc} */ public void endDisplay() { if (lastFocusComponent != null) { lastFocusComponent.requestFocusInWindow(); } }
public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_TAB) { if (selComp == text || selComp == text2) { selComp.setBackground(Color.WHITE); ((JTextField) selComp).setEditable(false); ((JTextField) selComp).getCaret().setVisible(false); } text2.setBackground(Color.WHITE); // list.setSelectedIndex(-1); cBox.repaint(); // if (saveAs) // text2.setBackground(Color.WHITE); if (selComp == cBox) cBox.setRequestFocusEnabled(false); if (selComp instanceof MyButton) { ((MyButton) selComp).setSelected(false); } // selComp.setRequestFocusEnabled(false); selComp.setFocusable(false); // list.setSelectedIndex(-1); if (!shift) selComp = (JComponent) mtp.getComponentAfter(dialog, selComp); else selComp = (JComponent) mtp.getComponentBefore(dialog, selComp); if (selComp == butCopy && !saveAs) if (!shift) selComp = (JComponent) mtp.getComponentAfter(dialog, selComp); else selComp = (JComponent) mtp.getComponentBefore(dialog, selComp); if (selComp == text || selComp == text2) { selComp.setBackground(vLightBlue); ((JTextField) selComp).getCaret().setVisible(true); ((JTextField) selComp).setEditable(true); } if (selComp == null) { selComp = list; } else if (selComp instanceof MyButton) ((MyButton) selComp).setSelected(true); if (selComp instanceof MyComboBox) { cBox.setBackground(vLightBlue); } selComp.setFocusable(true); selComp.requestFocusInWindow(); } else if (e.getKeyCode() == KeyEvent.VK_SHIFT) { shift = true; } else if (e.getKeyCode() == KeyEvent.VK_ENTER) { // if (selComp instanceof JList || selComp == text || selComp == // text2) { enterAction.actionPerformed(new ActionEvent(e, 0, "")); // } } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { // if (selComp instanceof JList) { cancelAction.actionPerformed(new ActionEvent(e, 0, "")); // } } else if (e.getKeyCode() == KeyEvent.VK_DELETE) { if (selComp instanceof JList) { deleteAction.actionPerformed(new ActionEvent(e, 0, "")); } } else if (selComp == cBox && ((e.getKeyCode() == KeyEvent.VK_UP) && driver.allFiles || (e.getKeyCode() == KeyEvent.VK_DOWN) && !driver.allFiles)) { driver.allFiles = !driver.allFiles; cBox.setSelectedIndex(driver.allFiles ? 1 : 0); return; } // else if (selComp == text || selComp == text2) { // selComp = (Component) e.getSource(); // } // repaint(); // String u = list.getSelectedValue(); // force selection // list.setSelectedValue(u, false); paintList(); list.repaint(); repaint(); }
/** * Sets the help text for the specified component. * * @param cont input container */ static void focus(final Component cont) { final GUI gui = gui(cont); if (gui == null) return; if (gui.gopts.get(GUIOptions.MOUSEFOCUS) && cont.isEnabled()) cont.requestFocusInWindow(); }
/** Ensures that the options dialog has no mutually exclusive options. */ private void verifyOptions(Object src) { // record GUI state boolean autoscaleEnabled = autoscaleBox.isEnabled(); boolean colorModeEnabled = colorModeChoice.isEnabled(); boolean concatenateEnabled = concatenateBox.isEnabled(); boolean cropEnabled = cropBox.isEnabled(); boolean groupFilesEnabled = groupFilesBox.isEnabled(); boolean ungroupFilesEnabled = ungroupFilesBox.isEnabled(); boolean openAllSeriesEnabled = openAllSeriesBox.isEnabled(); // boolean recordEnabled = recordBox.isEnabled(); boolean showMetadataEnabled = showMetadataBox.isEnabled(); boolean showOMEXMLEnabled = showOMEXMLBox.isEnabled(); boolean specifyRangesEnabled = specifyRangesBox.isEnabled(); boolean splitZEnabled = splitZBox.isEnabled(); boolean splitTEnabled = splitTBox.isEnabled(); boolean splitCEnabled = splitCBox.isEnabled(); // boolean stackFormatEnabled = stackFormatChoice.isEnabled(); boolean stackOrderEnabled = stackOrderChoice.isEnabled(); boolean swapDimsEnabled = swapDimsBox.isEnabled(); boolean virtualEnabled = virtualBox.isEnabled(); boolean isAutoscale = autoscaleBox.getState(); String colorModeValue = colorModeChoice.getSelectedItem(); boolean isConcatenate = concatenateBox.getState(); boolean isCrop = cropBox.getState(); boolean isGroupFiles = groupFilesBox.getState(); boolean isUngroupFiles = ungroupFilesBox.getState(); boolean isOpenAllSeries = openAllSeriesBox.getState(); // boolean isRecord = recordBox.getState(); boolean isShowMetadata = showMetadataBox.getState(); boolean isShowOMEXML = showOMEXMLBox.getState(); boolean isSpecifyRanges = specifyRangesBox.getState(); boolean isSplitZ = splitZBox.getState(); boolean isSplitT = splitTBox.getState(); boolean isSplitC = splitCBox.getState(); String stackFormatValue = stackFormatChoice.getSelectedItem(); boolean isStackNone = stackFormatValue.equals(ImporterOptions.VIEW_NONE); boolean isStackStandard = stackFormatValue.equals(ImporterOptions.VIEW_STANDARD); boolean isStackHyperstack = stackFormatValue.equals(ImporterOptions.VIEW_HYPERSTACK); boolean isStackBrowser = stackFormatValue.equals(ImporterOptions.VIEW_BROWSER); boolean isStackImage5D = stackFormatValue.equals(ImporterOptions.VIEW_IMAGE_5D); boolean isStackView5D = stackFormatValue.equals(ImporterOptions.VIEW_VIEW_5D); String stackOrderValue = stackOrderChoice.getSelectedItem(); boolean isSwap = swapDimsBox.getState(); boolean isVirtual = virtualBox.getState(); // toggle availability of each option based on state of earlier options // NB: The order the options are examined here defines their order of // precedence. This ordering is necessary because it affects which // component states are capable of graying out other components. // For example, we want to disable autoscaleBox when virtualBox is checked, // so the virtualBox logic must appear before the autoscaleBox logic. // To make it more intuitive for the user, the order of precedence should // match the component layout from left to right, top to bottom, according // to subsection. // == Stack viewing == // stackOrderChoice stackOrderEnabled = isStackStandard; if (src == stackFormatChoice) { if (isStackHyperstack || isStackBrowser || isStackImage5D) { stackOrderValue = ImporterOptions.ORDER_XYCZT; } else if (isStackView5D) stackOrderValue = ImporterOptions.ORDER_XYZCT; else stackOrderValue = ImporterOptions.ORDER_DEFAULT; } // == Metadata viewing == // showMetadataBox showMetadataEnabled = !isStackNone; if (!showMetadataEnabled) isShowMetadata = true; // showOMEXMLBox // NB: no other options affect showOMEXMLBox // == Dataset organization == // groupFilesBox if (src == stackFormatChoice && isStackBrowser) { isGroupFiles = true; } else if (!options.isLocal()) { isGroupFiles = false; groupFilesEnabled = false; } // ungroupFilesBox if (options.isOMERO()) { isUngroupFiles = false; ungroupFilesEnabled = false; } // swapDimsBox // NB: no other options affect swapDimsBox // openAllSeriesBox // NB: no other options affect openAllSeriesBox // concatenateBox // NB: no other options affect concatenateBox // == Memory management == // virtualBox virtualEnabled = !isStackNone && !isStackImage5D && !isStackView5D && !isConcatenate; if (!virtualEnabled) isVirtual = false; else if (src == stackFormatChoice && isStackBrowser) isVirtual = true; // recordBox // recordEnabled = isVirtual; // if (!recordEnabled) isRecord = false; // specifyRangesBox specifyRangesEnabled = !isStackNone && !isVirtual; if (!specifyRangesEnabled) isSpecifyRanges = false; // cropBox cropEnabled = !isStackNone && !isVirtual; if (!cropEnabled) isCrop = false; // == Color options == // colorModeChoice colorModeEnabled = !isStackImage5D && !isStackView5D && !isStackStandard; if (!colorModeEnabled) colorModeValue = ImporterOptions.COLOR_MODE_DEFAULT; // autoscaleBox autoscaleEnabled = !isVirtual; if (!autoscaleEnabled) isAutoscale = false; // == Split into separate windows == boolean splitEnabled = !isStackNone && !isStackBrowser && !isStackImage5D && !isStackView5D; // TODO: Make splitting work with Data Browser. // splitCBox splitCEnabled = splitEnabled; if (!splitCEnabled) isSplitC = false; // splitZBox splitZEnabled = splitEnabled; if (!splitZEnabled) isSplitZ = false; // splitTBox splitTEnabled = splitEnabled; if (!splitTEnabled) isSplitT = false; // update state of each option, in case anything changed autoscaleBox.setEnabled(autoscaleEnabled); colorModeChoice.setEnabled(colorModeEnabled); concatenateBox.setEnabled(concatenateEnabled); cropBox.setEnabled(cropEnabled); groupFilesBox.setEnabled(groupFilesEnabled); ungroupFilesBox.setEnabled(ungroupFilesEnabled); openAllSeriesBox.setEnabled(openAllSeriesEnabled); // recordBox.setEnabled(recordEnabled); showMetadataBox.setEnabled(showMetadataEnabled); showOMEXMLBox.setEnabled(showOMEXMLEnabled); specifyRangesBox.setEnabled(specifyRangesEnabled); splitZBox.setEnabled(splitZEnabled); splitTBox.setEnabled(splitTEnabled); splitCBox.setEnabled(splitCEnabled); // stackFormatChoice.setEnabled(stackFormatEnabled); stackOrderChoice.setEnabled(stackOrderEnabled); swapDimsBox.setEnabled(swapDimsEnabled); virtualBox.setEnabled(virtualEnabled); autoscaleBox.setState(isAutoscale); colorModeChoice.select(colorModeValue); concatenateBox.setState(isConcatenate); cropBox.setState(isCrop); groupFilesBox.setState(isGroupFiles); ungroupFilesBox.setState(isUngroupFiles); openAllSeriesBox.setState(isOpenAllSeries); // recordBox.setState(isRecord); showMetadataBox.setState(isShowMetadata); showOMEXMLBox.setState(isShowOMEXML); specifyRangesBox.setState(isSpecifyRanges); splitZBox.setState(isSplitZ); splitTBox.setState(isSplitT); splitCBox.setState(isSplitC); // stackFormatChoice.select(stackFormatValue); stackOrderChoice.select(stackOrderValue); swapDimsBox.setState(isSwap); virtualBox.setState(isVirtual); if (IS_GLITCHED) { // HACK - work around a Mac OS X bug where GUI components do not update // list of affected components Component[] c = { autoscaleBox, colorModeChoice, concatenateBox, cropBox, groupFilesBox, ungroupFilesBox, openAllSeriesBox, // recordBox, showMetadataBox, showOMEXMLBox, specifyRangesBox, splitZBox, splitTBox, splitCBox, stackFormatChoice, stackOrderChoice, swapDimsBox, virtualBox }; // identify currently focused component Component focused = null; for (int i = 0; i < c.length; i++) { if (c[i].isFocusOwner()) focused = c[i]; } // temporarily disable focus events for (int i = 0; i < c.length; i++) c[i].removeFocusListener(this); // cycle through focus on all components for (int i = 0; i < c.length; i++) c[i].requestFocusInWindow(); // clear the focus globally KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); kfm.clearGlobalFocusOwner(); sleep(100); // doesn't work if this value is too small // refocus the originally focused component if (focused != null) focused.requestFocusInWindow(); // reenable focus events for (int i = 0; i < c.length; i++) c[i].addFocusListener(this); } }