CopyOptionPane.java :  » Content-Management-System » contelligent » de » finix » contelligent » client » util » Java Open Source

Java Open Source » Content Management System » contelligent 
contelligent » de » finix » contelligent » client » util » CopyOptionPane.java
/*
 * Copyright 2001-2006 C:1 Financial Services GmbH
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License Version 2.1, as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
 */

package de.finix.contelligent.client.util;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.dnd.DnDConstants;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

import de.finix.contelligent.client.ContelligentClient;
import de.finix.contelligent.client.base.ContelligentComponent;
import de.finix.contelligent.client.base.ContelligentConstants;
import de.finix.contelligent.client.i18n.Resources;
import de.finix.contelligent.client.modules.preferences.PreferencesModule;

public class CopyOptionPane extends JOptionPane {

    private JRadioButton copyAction, movingCopyAction, moveAction, linkAction;

    private RestrictedTextField pathField;

    private JOptionPane optionPane;

    private ActionListener actionListener;

    public CopyOptionPane() {
        super();
        optionPane = this;
    }

    public int showCopyDialog(String title, String label, String defaultPath, int action, boolean isFinal) {
        return showCopyDialog(title, label, defaultPath, action, isFinal, DnDConstants.ACTION_COPY_OR_MOVE
                | DnDConstants.ACTION_LINK);
    }

    public int showCopyDialog(String title, String label, final String defaultPath, int action, boolean isFinal,
            int actions) {
        JLabel pathLabel = new JLabel(label);
        ButtonGroup actionButtons = new ButtonGroup();
        copyAction = new JRadioButton(Resources.getLocalString("copy"));
        movingCopyAction = new JRadioButton(Resources.getLocalString("moving_copy"));
        moveAction = new JRadioButton(Resources.getLocalString("move"));
        linkAction = new JRadioButton(Resources.getLocalString("create_link"));
        actionButtons.add(copyAction);
        if (PreferencesModule.getPreferences().getBoolean(PreferencesModule.ADVANCED_COPY,
                PreferencesModule.DEFAULT_ADVANCED_COPY)) {
            actionButtons.add(movingCopyAction);
        }
        actionButtons.add(moveAction);
        actionButtons.add(linkAction);

        if (isFinal) {
            action = DnDConstants.ACTION_LINK;
            linkAction.setEnabled(false);
            moveAction.setEnabled(false);
            movingCopyAction.setEnabled(false);
            copyAction.setEnabled(false);
        } else {
            linkAction.setEnabled((actions & DnDConstants.ACTION_LINK) != 0);
            moveAction.setEnabled((actions & DnDConstants.ACTION_MOVE) != 0);
            movingCopyAction.setEnabled((actions & DnDConstants.ACTION_COPY) != 0);
            copyAction.setEnabled((actions & DnDConstants.ACTION_COPY) != 0);
        }

        if (action == DnDConstants.ACTION_COPY) {
            copyAction.setSelected(true);
        } else if (action == DnDConstants.ACTION_MOVE) {
            moveAction.setSelected(true);
        } else {
            linkAction.setSelected(true);
        }

        Object[] array;
        if (defaultPath != null) {
            pathField = new RestrictedTextField(defaultPath);
            pathField.setAllowString(ContelligentComponent.getAllowString());
            if (PreferencesModule.getPreferences().getBoolean(PreferencesModule.ADVANCED_COPY,
                    PreferencesModule.DEFAULT_ADVANCED_COPY)) {
                array = new Object[] { pathLabel, pathField, copyAction, movingCopyAction, moveAction, linkAction };
            } else {
                array = new Object[] { pathLabel, pathField, copyAction, moveAction, linkAction };
            }
        } else {
            array = new Object[] { copyAction, moveAction };
        }

        setMessage(array);
        setMessageType(QUESTION_MESSAGE);
        setOptionType(JOptionPane.OK_CANCEL_OPTION);

        final EscapeDialog dialog = new EscapeDialog(ContelligentClient.getActiveFrame(), title, true);
        dialog.setContentPane(this);
        // dialog.registerExternal(this);

        addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent e) {
                String prop = e.getPropertyName();
                if (dialog.isVisible() && (e.getSource() == optionPane)
                        && (prop.equals(VALUE_PROPERTY) || prop.equals(INPUT_VALUE_PROPERTY))) {
                    Object value = getValue();
                    if (value == JOptionPane.UNINITIALIZED_VALUE) {
                        // ignore reset
                        return;
                    }

                    if (value instanceof Integer) {
                        int choosenValue = ((Integer) value).intValue();
                        if (choosenValue == JOptionPane.OK_OPTION) {
                            boolean validName = true;
                            if (defaultPath != null) {
                                // Check input
                                String text = pathField.getText();
                                for (int i = 0; i < text.length(); i++) {
                                    if (!Character.isLetterOrDigit(text.charAt(i))) {
                                        validName = false;
                                        for (int j = 0; j < ContelligentConstants.allowedCharacters.length; j++) {
                                            if (text.charAt(i) == ContelligentConstants.allowedCharacters[j]) {
                                                validName = true;
                                                break;
                                            }
                                        }
                                        if (!validName) {
                                            JOptionPane.showMessageDialog(null, Resources
                                                    .getLocalString("illegal_component_name_char")
                                                    + " '" + text.charAt(i) + "'", Resources
                                                    .getLocalString("illegal_component_name_title"),
                                                    JOptionPane.ERROR_MESSAGE);
                                            break;
                                        }
                                    }
                                }
                            }
                            if (validName) {
                                dialog.setVisible(false);
                            } else {
                                // Reset the JOptionPane's value.
                                // If you don't do this, then if the user
                                // presses the same button next time, no
                                // property change event will be fired.
                                optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                            }
                        } else if (choosenValue == JOptionPane.CANCEL_OPTION) {
                            dialog.setVisible(false);
                        }
                    }
                }
            }
        });

        dialog.pack();
        Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle winDim = getBounds();
        dialog.setLocation((screenDim.width - winDim.width) / 2, (screenDim.height - winDim.height) / 2);
        dialog.setVisible(true);
        if (getValue() instanceof Integer) {
            return ((Integer) getValue()).intValue();
        }
        return CANCEL_OPTION;
    }

    public String getPath() {
        return pathField.getText();
    }

    public int getAction() {
        if (copyAction.isSelected()) {
            return DnDConstants.ACTION_COPY;
        } else if (movingCopyAction.isSelected()) {
            // Slight misuse of the original meaning of the constant,
            // but we dont need it for anything else.
            return DnDConstants.ACTION_COPY_OR_MOVE;
        } else if (moveAction.isSelected()) {
            return DnDConstants.ACTION_MOVE;
        } else {
            return DnDConstants.ACTION_LINK;
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.