Example usage for javax.swing JComponent getClientProperty

List of usage examples for javax.swing JComponent getClientProperty

Introduction

In this page you can find the example usage for javax.swing JComponent getClientProperty.

Prototype

public final Object getClientProperty(Object key) 

Source Link

Document

Returns the value of the property with the specified key.

Usage

From source file:GUI.MainWindow.java

/**
 * @param args the command line arguments
 */// w w w. j  a  v  a2s  .c om
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {

        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    }
    //</editor-fold>

    // Create right click context menu for most Text Components
    final JPopupMenu copypaste = new JPopupMenu();

    JMenuItem cut = new JMenuItem(new DefaultEditorKit.CutAction());
    cut.setText("Cut");
    copypaste.add(cut);

    JMenuItem copy = new JMenuItem(new DefaultEditorKit.CopyAction());
    copy.setText("Copy");
    copypaste.add(copy);

    JMenuItem paste = new JMenuItem(new DefaultEditorKit.PasteAction());
    paste.setText("Paste");
    copypaste.add(paste);

    // Taken from here. It succinctly added a right click context menu to every text component.
    // http://stackoverflow.com/questions/19424574/adding-a-context-menu-to-all-swing-text-components-in-application
    javax.swing.UIManager.addAuxiliaryLookAndFeel(new LookAndFeel() {
        private final UIDefaults defaults = new UIDefaults() {
            public javax.swing.plaf.ComponentUI getUI(JComponent c) {
                if (c instanceof javax.swing.text.JTextComponent) {
                    if (c.getClientProperty(this) == null) {
                        c.setComponentPopupMenu(copypaste);
                        c.putClientProperty(this, Boolean.TRUE);
                    }
                }
                return null;
            }
        };

        @Override
        public UIDefaults getDefaults() {
            return defaults;
        }

        ;

        @Override
        public String getID() {
            return "TextContextMenu";
        }

        @Override
        public String getName() {
            return getID();
        }

        @Override
        public String getDescription() {
            return getID();
        }

        @Override
        public boolean isNativeLookAndFeel() {
            return false;
        }

        @Override
        public boolean isSupportedLookAndFeel() {
            return true;
        }
    });

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MainWindow().setVisible(true);
        }
    });
}

From source file:Main.java

private static TextLayout createTextLayout(JComponent c, String s, Font f, FontRenderContext frc) {
    Object shaper = (c == null ? null : c.getClientProperty(TextAttribute.NUMERIC_SHAPING));
    if (shaper == null) {
        return new TextLayout(s, f, frc);
    } else {//  w w  w.java 2s.  c om
        Map<TextAttribute, Object> a = new HashMap<TextAttribute, Object>();
        a.put(TextAttribute.FONT, f);
        a.put(TextAttribute.NUMERIC_SHAPING, shaper);
        return new TextLayout(s, a, frc);
    }
}

From source file:Main.java

public static Object getClientProperty(final JComponent jComponent, final Object key, final Object value) {
    return jComponent != null && key != null ? runInEDT(() -> jComponent.getClientProperty(key), null, value)
            : value;// w w w.  ja va 2  s  .  c om
}

From source file:Main.java

public static Object getClientProperty(final JComponent jComponent, final Object key, final Object value) {
    if (jComponent != null && key != null) {
        if (SwingUtilities.isEventDispatchThread()) {
            Object clientProperty = jComponent.getClientProperty(key);

            if (clientProperty == null) {
                clientProperty = value;//from   ww  w  .jav  a 2  s . co m
            }

            return clientProperty;
        } else {
            final Object[] clientProperty = new Object[] { value };

            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        clientProperty[0] = jComponent.getClientProperty(key);
                    }
                });
            } catch (InterruptedException | InvocationTargetException e) {

            }

            if (clientProperty[0] == null) {
                clientProperty[0] = value;
            }

            return clientProperty[0];
        }
    } else {
        return value;
    }
}

From source file:com.aw.swing.mvp.ui.painter.PainterMessages.java

private static JComponent getComponentThatRecivedFocus(Throwable exception) {
    BindingComponent bindingComponent = null;
    if (exception instanceof AWException) {
        if (exception instanceof AWValidationException) {
            if (((AWValidationException) exception).getListObject() != null
                    && ((AWValidationException) exception).getListObject().size() > 0) {
                List exceptions = ((AWValidationException) exception).getListObject();
                for (Iterator iterator = exceptions.iterator(); iterator.hasNext();) {
                    AWValidationException o = (AWValidationException) iterator.next();
                    if (o.getJComponent() != null) {
                        bindingComponent = (BindingComponent) (o.getJComponent())
                                .getClientProperty(BindingComponent.ATTR_BND);
                        if (!bindingComponent.isUiReadOnly()) {
                            return bindingComponent.getJComponent();
                        }//from w  w w  . j  av a 2s  .  co m
                    }
                }
            }
        }
        if ((exception instanceof AWSwingException) && ((AWSwingException) exception).getJComponent() != null) {
            JComponent jComponent = ((AWSwingException) exception).getJComponent();
            bindingComponent = (BindingComponent) jComponent.getClientProperty(BindingComponent.ATTR_BND);
            if (!bindingComponent.isUiReadOnly()) {
                return bindingComponent.getJComponent();
            }
        }

    }

    if (oldComponents.size() > 0) {
        for (Iterator iterator = oldComponents.iterator(); iterator.hasNext();) {
            bindingComponent = (BindingComponent) iterator.next();
            if (!bindingComponent.isUiReadOnly()) {
                return bindingComponent.getJComponent();
            }
        }
    }

    return null;
}

From source file:com.aw.swing.mvp.validation.support.AWInputVerifier.java

public boolean verify(JComponent input) {
    BndIJTextField bindingComponent = (BndIJTextField) input.getClientProperty(BindingComponent.ATTR_BND);
    try {//from  ww w  .j  a  v  a 2  s  .  c o m
        if (enable) {
            logger.debug("validate <" + bindingComponent.getFieldName() + ">");
            Presenter currentPst = AWWindowsManager.instance().getCurrentPst();
            if (currentPst != null) {
                currentPst.getValidatorMgr().validate(input);
            }
        }
    } catch (AWValidationException ex) {
        logger.info("validate - paint <" + bindingComponent.getFieldName() + ">");
        ex.setRequestFocus(false);
        PainterMessages.paintException(ex, false);
        //            PainterMessages.paintException(ex);
        //            return false;
        return true;
    }
    if (enable) {
        logger.debug("validate - clean <" + bindingComponent.getFieldName() + ">");
        if (!bindingComponent.isUiReadOnly()) {
            PainterMessages.cleanException(input);
        }
    }
    return true;
}

From source file:com.aw.swing.mvp.binding.component.support.BindingBuilder.java

public void buildBinding() {
    //        List<Field> fields = presenter.getIpView().getAttributesFromView();
    List<JComponent> components = presenter.getIpView().getCmpsFromView();
    logger.debug(" JTextField and JCheckBox finds ..<" + components.size() + ">");
    Object vsr = presenter.getViewMgr().getViewSrc();
    for (JComponent component : components) {
        Field field = (Field) component.getClientProperty("Field");
        logger.debug(" execute binding for <" + field.getName() + ">");
        if (component == null) {
            throw new IllegalArgumentException("It does not exist JComponent for " + field.getName());
        }//from w  w w. j  a va2s . co  m
        BindingComponent bn = (BindingComponent) component.getClientProperty(BindingComponent.ATTR_BND);
        if (bn == null || bn.getFieldName() == null) {
            logger.debug(" executing automatic binding for <" + field.getName() + ">");
            buildBindingFor(field, component);
        }
    }
    int index = -1;
    for (BindingComponent bindingComponent : bindingComponents) {
        //            if(bindingComponent==null) continue;
        index = presenter.getBindingMgr().getCurrentInputCmpMgr().getComponents().indexOf(bindingComponent);
        if (index != -1) {
            ((BindingComponent) presenter.getBindingMgr().getCurrentInputCmpMgr().getComponents().get(index))
                    .setFieldName(bindingComponent.getFieldName());
        } else {
            presenter.getBindingMgr().getCurrentInputCmpMgr().addComponent(bindingComponent);
        }
    }
}

From source file:com.aw.swing.mvp.cmp.pick.PickManager.java

/**
 * @param attrName La caja de texto txt<attrName> debe existir
 * @param pick/*from   w w  w  .j  a v  a2  s. c  o m*/
 * @return
 */
public Pick registerPick(String attrName, final Pick pick) {
    final String pickName = getPickName(attrName);
    getPicksInfo().add(new PickInfo(pick, attrName, pickName));
    picks.add(pick);
    if (pick instanceof PickImpl) {
        ((PickImpl) pick).setPresenter(presenter);
        pick.setMainAttribute(attrName);
        final JTextComponent jTextComponent = (JTextComponent) presenter.getIpView()
                .getComponent(getTxtPick(attrName));
        jTextComponent.addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent e) {

                if (e.isTemporary()) {
                    return;
                }

                // if the pick action is currently executing
                Boolean executingPick = (Boolean) jTextComponent
                        .getClientProperty(BindingComponent.ATTR_EXECUTING_PICK_ACTION);
                if ((executingPick != null) && (executingPick)) {
                    jTextComponent.putClientProperty(BindingComponent.ATTR_EXECUTING_PICK_ACTION, null);
                    //                        return;
                }
                System.out.println("XXX    Focus LOST   2");
                // if the focus will be directed to the pick Button
                Component cmp = e.getOppositeComponent();
                if (cmp instanceof JComponent) {
                    JComponent jComponent = (JComponent) cmp;
                    String actionName = (String) jComponent.getClientProperty(BindingComponent.ATTR_ACTION);
                    if (pickName.equals(actionName)) {
                        return;
                    }
                }

                if (!pick.isPickFilled()) {

                    jTextComponent.setText("");
                }
            }
        });
        jTextComponent.addKeyListener(new PickKeyListener(pick));
        jTextComponent.putClientProperty(PICK_NAME, pickName);
        jTextComponent.putClientProperty(PICK, pick);
        //            JButton jButton = (JButton) presenter.getIpView().getComponent(getBtnPick(attrName));
    }
    return pick;
}

From source file:com.aw.swing.mvp.binding.BindingManager.java

public BindingComponent getBindingComponent(JComponent component) {
    return (BindingComponent) component.getClientProperty(BindingComponent.ATTR_BND);
}

From source file:com.aw.swing.mvp.action.ActionManager.java

public void closeAllView(Action action, Object actionResult) {
    FlowManager flowManager = AWWindowsManager.instance().getCurrentFlowMgr();
    List<Flow> flows = flowManager.getFlows();
    if (flows.size() == 0) {
        action.getPst().closeView();//from   w ww  . j  av  a2s  .  c o  m
        return;
    }
    // workaround para evitar un bug al momento de llamar los rechazarpst 
    boolean forceClosing = false;
    Object cmpAction = action.getJComponent();
    if (cmpAction != null && cmpAction instanceof JComponent) {
        JComponent jcmp = (JComponent) cmpAction;
        forceClosing = jcmp.getClientProperty("forceClosing") != null;
    }
    if (!forceClosing && flows.size() == 1) {
        closeView(action, actionResult);
        return;
    }

    flowManager.removeAllFlows();
    flowManager.addFlow(flows.get(0));

    Flow flow = getNewFlowToCloseView(action);
    flow.setAttributes(action.getAttributesAtCloseView());
    flow.setAttribute(Flow.RESULT_ACTION, actionResult);
    flow.setAttribute("resultMsg", action.getResultMsg());
    flowManager.closeSecondaryPresenters();
}