Example usage for javax.swing SwingUtilities getAncestorOfClass

List of usage examples for javax.swing SwingUtilities getAncestorOfClass

Introduction

In this page you can find the example usage for javax.swing SwingUtilities getAncestorOfClass.

Prototype

public static Container getAncestorOfClass(Class<?> c, Component comp) 

Source Link

Document

Convenience method for searching above comp in the component hierarchy and returns the first object of class c it finds.

Usage

From source file:com.sshtools.common.ui.SshToolsConnectionPanel.java

/**
 *
 *
 * @param parent//  www .j a v  a 2s.  c  om
 * @param profile
 * @param optionalTabs
 *
 * @return
 */
public static SshToolsConnectionProfile showConnectionDialog(Component parent,
        SshToolsConnectionProfile profile, SshToolsConnectionTab[] optionalTabs) {
    //  If no properties are provided, then use the default
    if (profile == null) {
        int port = DEFAULT_PORT;
        String port_S = Integer.toString(DEFAULT_PORT);
        port_S = PreferencesStore.get(SshTerminalPanel.PREF_DEFAULT_SIMPLE_PORT, port_S);
        try {
            port = Integer.parseInt(port_S);
        } catch (NumberFormatException e) {
            log.warn("Could not parse the port number from defaults file (property name"
                    + SshTerminalPanel.PREF_DEFAULT_SIMPLE_PORT + ", property value= " + port_S + ").");
        }
        profile = new SshToolsConnectionProfile();
        profile.setHost(PreferencesStore.get(SshToolsApplication.PREF_CONNECTION_LAST_HOST, ""));
        profile.setPort(PreferencesStore.getInt(SshToolsApplication.PREF_CONNECTION_LAST_PORT, port));
        profile.setUsername(PreferencesStore.get(SshToolsApplication.PREF_CONNECTION_LAST_USER, ""));

    }

    final SshToolsConnectionPanel conx = new SshToolsConnectionPanel(true);

    if (optionalTabs != null) {
        for (int i = 0; i < optionalTabs.length; i++) {
            conx.addTab(optionalTabs[i]);
        }
    }

    conx.setConnectionProfile(profile);

    JDialog d = null;
    Window w = (Window) SwingUtilities.getAncestorOfClass(Window.class, parent);

    if (w instanceof JDialog) {
        d = new JDialog((JDialog) w, "Connection Profile", true);
    } else if (w instanceof JFrame) {
        d = new JDialog((JFrame) w, "Connection Profile", true);
    } else {
        d = new JDialog((JFrame) null, "Connection Profile", true);
    }

    final JDialog dialog = d;

    class UserAction {
        boolean connect;
    }

    final UserAction userAction = new UserAction();

    //  Create the bottom button panel
    final JButton cancel = new JButton("Cancel");
    cancel.setMnemonic('c');
    cancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            userAction.connect = false;
            dialog.setVisible(false);
        }
    });

    final JButton connect = new JButton("Connect");
    connect.setMnemonic('t');
    connect.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (conx.validateTabs()) {
                userAction.connect = true;
                dialog.setVisible(false);
            }
        }
    });
    dialog.getRootPane().setDefaultButton(connect);

    JPanel buttonPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.insets = new Insets(6, 6, 0, 0);
    gbc.weighty = 1.0;
    UIUtil.jGridBagAdd(buttonPanel, connect, gbc, GridBagConstraints.RELATIVE);
    UIUtil.jGridBagAdd(buttonPanel, cancel, gbc, GridBagConstraints.REMAINDER);

    JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
    southPanel.add(buttonPanel);

    //
    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    mainPanel.add(conx, BorderLayout.CENTER);
    mainPanel.add(southPanel, BorderLayout.SOUTH);

    // Show the dialog
    dialog.getContentPane().setLayout(new GridLayout(1, 1));
    dialog.getContentPane().add(mainPanel);
    dialog.pack();
    dialog.setResizable(false);
    UIUtil.positionComponent(SwingConstants.CENTER, dialog);

    //show the simple box and act on the answer.
    SshToolsSimpleConnectionPrompt stscp = SshToolsSimpleConnectionPrompt.getInstance();
    StringBuffer sb = new StringBuffer();
    userAction.connect = !stscp.getHostname(sb, profile.getHost());

    boolean advanced = stscp.getAdvanced();

    if (advanced) {
        userAction.connect = false;
        profile.setHost(sb.toString());
        conx.hosttab.setConnectionProfile(profile);
        dialog.setVisible(true);
    }

    // Make sure we didn't cancel
    if (!userAction.connect) {
        return null;
    }

    conx.applyTabs();
    if (!advanced)
        profile.setHost(sb.toString());
    if (!advanced) {
        int port = DEFAULT_PORT;
        String port_S = Integer.toString(DEFAULT_PORT);
        port_S = PreferencesStore.get(SshTerminalPanel.PREF_DEFAULT_SIMPLE_PORT, port_S);
        try {
            port = Integer.parseInt(port_S);
        } catch (NumberFormatException e) {
            log.warn("Could not parse the port number from defaults file (property name"
                    + SshTerminalPanel.PREF_DEFAULT_SIMPLE_PORT + ", property value= " + port_S + ").");
        }
        profile.setPort(port);
    }
    if (!advanced)
        profile.setUsername("");

    PreferencesStore.put(SshToolsApplication.PREF_CONNECTION_LAST_HOST, profile.getHost());
    // only save user inputed configuration
    if (advanced) {
        PreferencesStore.put(SshToolsApplication.PREF_CONNECTION_LAST_USER, profile.getUsername());
        PreferencesStore.putInt(SshToolsApplication.PREF_CONNECTION_LAST_PORT, profile.getPort());
    }
    // Return the connection properties
    return profile;
}

From source file:DataExchangeTest.java

/**
 * Show the chooser panel in a dialog//from  w  w  w  .java 2  s .  c om
 * @param parent a component in the owner frame or null
 * @param title the dialog window title
 */
public boolean showDialog(Component parent, String title) {
    ok = false;

    // locate the owner frame

    Frame owner = null;
    if (parent instanceof Frame)
        owner = (Frame) parent;
    else
        owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);

    // if first time, or if owner has changed, make new dialog

    if (dialog == null || dialog.getOwner() != owner) {
        dialog = new JDialog(owner, true);
        dialog.add(this);
        dialog.getRootPane().setDefaultButton(okButton);
        dialog.pack();
    }

    // set title and show dialog

    dialog.setTitle(title);
    dialog.setVisible(true);
    return ok;
}

From source file:com.hp.alm.ali.idea.content.taskboard.TaskBoardPanel.java

private <T extends Component> T locateContainer(MouseEvent event, Class<T> clazz) {
    Point p = SwingUtilities.convertPoint((Component) event.getSource(), event.getPoint(), TaskBoardPanel.this);
    Component comp = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
    return (T) SwingUtilities.getAncestorOfClass(clazz, comp);
}

From source file:com.qframework.core.GameonApp.java

private void onTextInput(final String resptype, final String respdata) {

    Timer t = new javax.swing.Timer(50, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JTextField area = new JTextField(resptype);
            area.setFocusable(true);//  w  ww .ja va  2s .  co m
            area.requestFocus();
            Object options[] = new Object[] { area };
            Frame parent = null;
            if (mAppContext != null)
                parent = (Frame) SwingUtilities.getAncestorOfClass(java.awt.Frame.class, mAppContext);
            else
                parent = (Frame) SwingUtilities.getAncestorOfClass(java.awt.Frame.class, mAppletContext);

            int option = JOptionPane.showOptionDialog(parent, options, "", JOptionPane.YES_OPTION,
                    JOptionPane.INFORMATION_MESSAGE, null, null, null);
            if (option == JOptionPane.YES_OPTION) {
                String truncated = area.getText().replaceAll("[^A-Za-z0-9:.@ ]", "");
                String script = respdata + "('" + truncated + "' , 1);";
                mScript.execScript(script);
            } else {
                String script = respdata + "(undefined, 1);";
                mScript.execScript(script);

            }
        }
    });
    t.setRepeats(false);
    t.start();

}

From source file:com.emr.schemas.SchemerMapper.java

/**
 * Handles click event for the Next button
 * Opens {@link TableRelationsForm} for the user to define the relationships between the selected tables.
 * @param evt {@link ActionEvent}/*from w ww  .  j  a va  2 s  .  c  o  m*/
 */
private void btnNextScreenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextScreenActionPerformed
    if (listOfTables.isEmpty()) {
        JOptionPane.showMessageDialog(this, "Please select source tables from thee list",
                "No selection was made", JOptionPane.ERROR_MESSAGE);
    } else {
        Container container = SwingUtilities.getAncestorOfClass(JDesktopPane.class,
                (Component) evt.getSource());

        if (container != null) {
            btnClearSel.setEnabled(false);
            btnNextScreen.setEnabled(false);
            JDesktopPane desktopPane = getDesktopPane();
            TableRelationsForm frm = new TableRelationsForm(listOfTables, this);
            desktopPane.add(frm);
            frm.setVisible(true);
            frm.setSize(800, 500);
            frm.setLocation(120, 60);
            frm.moveToFront();
            frm.addInternalFrameListener(new InternalFrameListener() {

                @Override
                public void internalFrameOpened(InternalFrameEvent e) {
                    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                }

                @Override
                public void internalFrameClosing(InternalFrameEvent e) {
                    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                }

                @Override
                public void internalFrameClosed(InternalFrameEvent e) {
                    btnClearSel.setEnabled(true);
                    btnNextScreen.setEnabled(true);
                }

                @Override
                public void internalFrameIconified(InternalFrameEvent e) {
                    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                }

                @Override
                public void internalFrameDeiconified(InternalFrameEvent e) {
                    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                }

                @Override
                public void internalFrameActivated(InternalFrameEvent e) {
                    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                }

                @Override
                public void internalFrameDeactivated(InternalFrameEvent e) {
                    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                }
            });
        }
    }

}

From source file:EditorDropTarget4.java

public void autoscroll(Point location) {
    JScrollPane scroller = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this);
    if (scroller != null) {
        JScrollBar hBar = scroller.getHorizontalScrollBar();
        JScrollBar vBar = scroller.getVerticalScrollBar();
        Rectangle r = getVisibleRect();
        if (location.x <= r.x + scrollInsets.left) {
            // Need to scroll left
            hBar.setValue(hBar.getValue() - hBar.getUnitIncrement(-1));
        }/*from  w  w w.  j a  v  a 2s.c om*/
        if (location.y <= r.y + scrollInsets.top) {
            // Need to scroll up
            vBar.setValue(vBar.getValue() - vBar.getUnitIncrement(-1));
        }
        if (location.x >= r.x + r.width - scrollInsets.right) {
            // Need to scroll right
            hBar.setValue(hBar.getValue() + hBar.getUnitIncrement(1));
        }
        if (location.y >= r.y + r.height - scrollInsets.bottom) {
            // Need to scroll down
            vBar.setValue(vBar.getValue() + vBar.getUnitIncrement(1));
        }
    }
}

From source file:com.sshtools.sshvnc.SshVNCPanel.java

protected int showAuthenticationPrompt(SshAuthenticationClient instance) throws

IOException {/*from  www  .  java 2  s  . c  o m*/

    // Get password from profile, if any ppp

    String profPw = getCurrentConnectionProfile()
            .getApplicationProperty(SshVNCPanel.PROFILE_PROPERTY_SSH_PASSWORD, "");

    if (!profPw.equals("")) {

        // SSH_PASSWORD is hexascii-encoded. Decode.

        ((PasswordAuthenticationClient) instance).setPassword(HexASCII.ToString(profPw));

    }

    instance.setUsername(getCurrentConnectionProfile().getUsername());

    if (instance instanceof PasswordAuthenticationClient) {

        PasswordAuthenticationDialog dialog =

                new PasswordAuthenticationDialog(

                        (Frame) SwingUtilities.getAncestorOfClass(

                                Frame.class, SshVNCPanel.this));

        instance.setAuthenticationPrompt(dialog);

        ((PasswordAuthenticationClient) instance).setPasswordChangePrompt(

                PasswordChange.getInstance());

        PasswordChange.getInstance().setParentComponent(

                SshVNCPanel.this);

    }

    else if (instance instanceof PublicKeyAuthenticationClient) {

        PublicKeyAuthenticationPrompt prompt = new PublicKeyAuthenticationPrompt(

                SshVNCPanel.this);

        instance.setAuthenticationPrompt(prompt);

    }

    else if (instance instanceof KBIAuthenticationClient) {

        KBIAuthenticationClient kbi = new KBIAuthenticationClient();

        ((KBIAuthenticationClient) instance).setKBIRequestHandler(

                new KBIRequestHandlerDialog(

                        (Frame) SwingUtilities.getAncestorOfClass(

                                Frame.class, SshVNCPanel.this)));

    }

    return ssh.authenticate(instance,
            getCurrentConnectionProfile().getApplicationProperty(PROFILE_PROPERTY_VNC_HOST, "localhost"));

}

From source file:net.chaosserver.timelord.swingui.CommonTaskPanel.java

/**
 * Shows the add time dialog to let the user add time to a task.
 *//*from ww  w.  ja va2 s. c  o  m*/
public void showAddTimeDialog() {
    JFrame frame = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this);

    AddTimeDialog addTimeDialog = new AddTimeDialog(frame, getTimelordData(), getDateDisplayed());
    addTimeDialog.setVisible(true);
    addTimeDialog.dispose();
}

From source file:com.sshtools.powervnc.PowerVNCPanel.java

protected int showAuthenticationPrompt(SshAuthenticationClient instance) throws

IOException {//from   w w  w .  j a  v a  2 s  .co  m

    // Get password from profile, if any ppp

    String profPw = getCurrentConnectionProfile()
            .getApplicationProperty(PowerVNCPanel.PROFILE_PROPERTY_SSH_PASSWORD, "");

    if (!profPw.equals("")) {

        // SSH_PASSWORD is hexascii-encoded. Decode.

        ((PasswordAuthenticationClient) instance).setPassword(HexASCII.ToString(profPw));

    }

    instance.setUsername(getCurrentConnectionProfile().getUsername());

    if (instance instanceof PasswordAuthenticationClient) {

        PasswordAuthenticationDialog dialog =

                new PasswordAuthenticationDialog(

                        (Frame) SwingUtilities.getAncestorOfClass(

                                Frame.class, PowerVNCPanel.this));

        instance.setAuthenticationPrompt(dialog);

        ((PasswordAuthenticationClient) instance).setPasswordChangePrompt(

                PasswordChange.getInstance());

        PasswordChange.getInstance().setParentComponent(

                PowerVNCPanel.this);

    }

    else if (instance instanceof PublicKeyAuthenticationClient) {

        PublicKeyAuthenticationPrompt prompt = new PublicKeyAuthenticationPrompt(

                PowerVNCPanel.this);

        instance.setAuthenticationPrompt(prompt);

    }

    else if (instance instanceof KBIAuthenticationClient) {

        KBIAuthenticationClient kbi = new KBIAuthenticationClient();

        ((KBIAuthenticationClient) instance).setKBIRequestHandler(

                new KBIRequestHandlerDialog(

                        (Frame) SwingUtilities.getAncestorOfClass(

                                Frame.class, PowerVNCPanel.this)));

    }

    return ssh.authenticate(instance,
            getCurrentConnectionProfile().getApplicationProperty(PROFILE_PROPERTY_VNC_HOST, "localhost"));

}

From source file:net.chaosserver.timelord.swingui.CommonTaskPanel.java

/**
 * Bring up a dialog to allow a user to edit the note associated with a
 * task.//w  ww  .  jav  a2  s .  co m
 *
 * @param timelordTask the task to edit the note on
 */
public void showEditNoteDialog(TimelordTask timelordTask) {
    // If the noteDialog doesn't exist, create it. But don't dispose
    // of it after use. It's simple enough that it's easy to re-use it.
    if (this.noteDialog == null) {
        JFrame frame = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this);
        this.noteDialog = new NoteDialog(frame);
    }

    noteDialog.setTimelordTask(timelordTask);
    noteDialog.setDate(getDateDisplayed());
    SwingUtil.repairLocation(noteDialog);
    noteDialog.setVisible(true);
}