Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package org.apache.cayenne.modeler.util;
 
 import java.awt.Color;
 import java.awt.Font;
 
 
Utility class to create standard Swing widgets following default look-and-feel of CayenneModeler.
 
 
 // TODO: (Andrus) investigate performance impact of substituting
 // constructors for all new widgets with cloning the prototype
 public class CayenneWidgetFactory {

    
Not intended for instantiation.
 
     protected CayenneWidgetFactory() {
         super();
     }

    
Creates a new JComboBox with a collection of model objects.
 
     public static JComboBox createComboBox(Collection<Stringmodelboolean sort) {
         return createComboBox(model.toArray(), sort);
     }

    
Creates a new JComboBox with an array of model objects.
 
     public static JComboBox createComboBox(Object[] modelboolean sort) {
         JComboBox comboBox = CayenneWidgetFactory.createComboBox();
 
         if (sort) {
             Arrays.sort(model);
         }
 
         comboBox.setModel(new DefaultComboBoxModel(model));
         return comboBox;
     }

    
Creates a new JComboBox.
 
     public static JComboBox createComboBox() {
         JComboBox comboBox = new JComboBox();
         initFormWidget(comboBox);
         comboBox.setBackground(.);
         return comboBox;
     }

    
Creates undoable JComboBox.
    public static JComboBox createUndoableComboBox() {
        JComboBox comboBox = new JComboBox();
        initFormWidget(comboBox);
        comboBox.addItemListener(new JComboBoxUndoListener());
        comboBox.setBackground(.);
        return comboBox;
    }

    
Creates undoable JTextField.
    public static JTextField createUndoableTextField() {
        return new JTextFieldUndoable();
    }

    
Creates undoable JTextField.
    public static JTextField createUndoableTextField(int size) {
        return new JTextFieldUndoable(size);
    }

    
Creates cell editor for text field
    public static DefaultCellEditor createCellEditor(JTextField textField) {
        return new CayenneCellEditor(textField);
    }

    
Creates cell editor for a table with combo as editor component. Type of this editor depends on auto-completion behavior of JComboBox

Parameters:
combo JComboBox to be used as editor component
    public static TableCellEditor createCellEditor(JComboBox combo) {
        if (..equals(combo
                .getClientProperty(.))) {
            return new ComboBoxCellEditor(combo);
        }
        DefaultCellEditor editor = new DefaultCellEditor(combo);
        editor.setClickCountToStart(1);
        return editor;
    }

    
Creates a new JTextField with a default columns count of 20.
    public static JTextField createTextField() {
        return createTextField(20);
    }

    
Creates a new JTextField with a specified columns count.
    public static JTextField createTextField(int columns) {
        final JTextField textField = new JTextField(columns);
        initFormWidget(textField);
        initTextField(textField);
        return textField;
    }
    protected static void initTextField(final JTextField textField) {
        // config focus
        textField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // transfer focus
                textField.transferFocus();
            }
        });
    }

    
Initializes a "form" element with a standard font and height.
    protected static void initFormWidget(JComponent component) {
        component.setFont(component.getFont().deriveFont(., 12));
        /*
         * Dimension size = component.getPreferredSize(); if (size == null) { size = new
         * Dimension(); } size.setSize(size.getWidth(), 20);
         * component.setPreferredSize(size);
         */
    }

    
Creates a borderless button that can be used as a clickable label.
    public static JButton createLabelButton(String text) {
        JButton but = createButton(text);
        but.setBorderPainted(false);
        but.setFocusPainted(false);
        but.setMargin(new Insets(0, 0, 0, 0));
        but.setBorder(null);
        return but;
    }

    
Creates a normal button.
    public static JButton createButton(String text) {
        return new JButton(text);
    }

    
Creates and returns a JEdit text component with syntax highlighing
    public static JEditTextArea createJEditTextArea() {
        JEditTextArea area = new JEditTextAreaUndoable();
        if (OperatingSystem.getOS() == .) {
            area.setInputHandler(new MacInputHandler());
        }
        return area;
    }
    // public static JSQLTextPane createJSQLTextPane() {
    // JSQLTextPane area = new JSQLTextPane();
    // return area;
    // }
    public static JCayenneTextPane createJEJBQLTextPane() {
        JCayenneTextPane area = new JCayenneTextPaneUndoable(new EJBQLSyntaxConstant());
        return area;
    }

    
Class for enabling Mac OS X keys
    private static class MacInputHandler extends DefaultInputHandler {
        MacInputHandler() {
            addDefaultKeyBindings();
        }

        
Sets up the default key bindings.
        public void addDefaultKeyBindings() {
            addKeyBinding("BACK_SPACE");
            addKeyBinding("M+BACK_SPACE");
            addKeyBinding("DELETE");
            addKeyBinding("M+DELETE");
            addKeyBinding("ENTER");
            addKeyBinding("TAB");
            addKeyBinding("INSERT");
            addKeyBinding("M+\\");
            addKeyBinding("HOME");
            addKeyBinding("END");
            addKeyBinding("M+A");
            addKeyBinding("S+HOME");
            addKeyBinding("S+END");
            addKeyBinding("M+HOME");
            addKeyBinding("M+END");
            addKeyBinding("MS+HOME");
            addKeyBinding("MS+END");
            addKeyBinding("PAGE_UP");
            addKeyBinding("PAGE_DOWN");
            addKeyBinding("S+PAGE_UP");
            addKeyBinding("S+PAGE_DOWN");
            addKeyBinding("LEFT");
            addKeyBinding("S+LEFT");
            addKeyBinding("A+LEFT"); // option + left
            addKeyBinding("AS+LEFT"); // option + shift + left
            addKeyBinding("RIGHT");
            addKeyBinding("S+RIGHT");
            addKeyBinding("A+RIGHT"); // option + right
            addKeyBinding("AS+RIGHT"); // option + shift + right
            addKeyBinding("UP");
            addKeyBinding("S+UP");
            addKeyBinding("DOWN");
            addKeyBinding("S+DOWN");
            addKeyBinding("M+ENTER");
            // Clipboard
            addKeyBinding("M+C"); // command + c
            addKeyBinding("M+V"); // command + v
            addKeyBinding("M+X"); // command + x
        }
    }
New to GrepCode? Check out our FAQ X