sandbox/java.hints/spi.java.hints/src/org/netbeans/modules/java/hints/providers/code/ReflectiveCustomizerProvider.java
branchdonation_review
changeset 1043 57843026e60b
parent 1027 205b7632914c
parent 1040 f7b6892fd754
child 1044 7feb751ba76b
     1.1 --- a/sandbox/java.hints/spi.java.hints/src/org/netbeans/modules/java/hints/providers/code/ReflectiveCustomizerProvider.java	Mon Dec 19 11:37:36 2016 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,261 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
     1.8 - *
     1.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    1.10 - * Other names may be trademarks of their respective owners.
    1.11 - *
    1.12 - * The contents of this file are subject to the terms of either the GNU
    1.13 - * General Public License Version 2 only ("GPL") or the Common
    1.14 - * Development and Distribution License("CDDL") (collectively, the
    1.15 - * "License"). You may not use this file except in compliance with the
    1.16 - * License. You can obtain a copy of the License at
    1.17 - * http://www.netbeans.org/cddl-gplv2.html
    1.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.19 - * specific language governing permissions and limitations under the
    1.20 - * License.  When distributing the software, include this License Header
    1.21 - * Notice in each file and include the License file at
    1.22 - * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    1.23 - * particular file as subject to the "Classpath" exception as provided
    1.24 - * by Oracle in the GPL Version 2 section of the License file that
    1.25 - * accompanied this code. If applicable, add the following below the
    1.26 - * License Header, with the fields enclosed by brackets [] replaced by
    1.27 - * your own identifying information:
    1.28 - * "Portions Copyrighted [year] [name of copyright owner]"
    1.29 - *
    1.30 - * If you wish your version of this file to be governed by only the CDDL
    1.31 - * or only the GPL Version 2, indicate your decision by adding
    1.32 - * "[Contributor] elects to include this software in this distribution
    1.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.34 - * single choice of license, a recipient has the option to distribute
    1.35 - * your version of this file under either the CDDL, the GPL Version 2 or
    1.36 - * to extend the choice of license to its licensees as provided above.
    1.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.38 - * Version 2 license, then the option applies only if the new code is
    1.39 - * made subject to such option by the copyright holder.
    1.40 - *
    1.41 - * Contributor(s):
    1.42 - *
    1.43 - * Portions Copyrighted 2012 Sun Microsystems, Inc.
    1.44 - */
    1.45 -package org.netbeans.modules.java.hints.providers.code;
    1.46 -
    1.47 -import java.awt.GridBagConstraints;
    1.48 -import java.awt.GridBagLayout;
    1.49 -import java.awt.Insets;
    1.50 -import java.awt.event.ActionEvent;
    1.51 -import java.awt.event.ActionListener;
    1.52 -import java.util.List;
    1.53 -import java.util.prefs.Preferences;
    1.54 -import javax.swing.JCheckBox;
    1.55 -import javax.swing.JComponent;
    1.56 -import javax.swing.JFormattedTextField;
    1.57 -import javax.swing.JLabel;
    1.58 -import javax.swing.JPanel;
    1.59 -import javax.swing.JSpinner;
    1.60 -import javax.swing.SpinnerNumberModel;
    1.61 -import javax.swing.event.ChangeEvent;
    1.62 -import javax.swing.event.ChangeListener;
    1.63 -import javax.swing.text.NumberFormatter;
    1.64 -import org.netbeans.spi.java.hints.BooleanOption;
    1.65 -import org.netbeans.spi.java.hints.CustomizerProvider;
    1.66 -import org.netbeans.spi.java.hints.IntegerOption;
    1.67 -import org.openide.util.Exceptions;
    1.68 -
    1.69 -/**
    1.70 - *
    1.71 - * @author lahvac
    1.72 - */
    1.73 -public class ReflectiveCustomizerProvider implements CustomizerProvider {
    1.74 -    private final String hintClassName;
    1.75 -    private final String hintId;
    1.76 -    private final List<OptionDescriptor> options;
    1.77 -
    1.78 -    public ReflectiveCustomizerProvider(String hintClassName, String hintId, List<OptionDescriptor> options) {
    1.79 -        this.hintClassName = hintClassName;
    1.80 -        this.hintId = hintId;
    1.81 -        this.options = options;
    1.82 -    }
    1.83 -
    1.84 -    @Override
    1.85 -    public JComponent getCustomizer(Preferences prefs) {
    1.86 -        return new CustomizerImpl(prefs, hintClassName, hintId, options);
    1.87 -    }
    1.88 -
    1.89 -    private static final class CustomizerImpl extends JPanel {
    1.90 -        private int row;
    1.91 -        
    1.92 -        public CustomizerImpl(Preferences prefs, String hintClassName, String hintId, List<OptionDescriptor> options) {
    1.93 -            try {
    1.94 -                setLayout(new GridBagLayout());
    1.95 -                row = 0;
    1.96 -                
    1.97 -                for (OptionDescriptor option : options) {
    1.98 -                    if (option.parameters instanceof IntegerOption) {
    1.99 -                        createIntegerOption(option, prefs);
   1.100 -                    }
   1.101 -                }
   1.102 -
   1.103 -                for (OptionDescriptor option : options) {
   1.104 -                    if (option.parameters instanceof BooleanOption) {
   1.105 -                        createBooleanOption(option, prefs);
   1.106 -                    }
   1.107 -                }
   1.108 -
   1.109 -                GridBagConstraints constraints = new GridBagConstraints();
   1.110 -
   1.111 -                constraints.anchor = GridBagConstraints.NORTHWEST;
   1.112 -                constraints.fill = GridBagConstraints.BOTH;
   1.113 -                constraints.gridheight = 1;
   1.114 -                constraints.gridwidth = GridBagConstraints.REMAINDER;
   1.115 -                constraints.gridx = 0;
   1.116 -                constraints.gridy = row++;
   1.117 -                constraints.weightx = 1;
   1.118 -                constraints.weighty = 1;
   1.119 -
   1.120 -                add(new JPanel(), constraints);
   1.121 -            } catch (IllegalArgumentException ex) {
   1.122 -                Exceptions.printStackTrace(ex);
   1.123 -            } catch (SecurityException ex) {
   1.124 -                Exceptions.printStackTrace(ex);
   1.125 -            }
   1.126 -        }
   1.127 -        
   1.128 -        private void createIntegerOption(OptionDescriptor option, Preferences prefs) {
   1.129 -            IntegerOption iopt = (IntegerOption)option.parameters;
   1.130 -            JLabel l = new JLabel();
   1.131 -            org.openide.awt.Mnemonics.setLocalizedText(l, option.displayName + ":");
   1.132 -            
   1.133 -            GridBagConstraints constraints = new GridBagConstraints();
   1.134 -            constraints.anchor = GridBagConstraints.WEST;
   1.135 -            constraints.fill = GridBagConstraints.NONE;
   1.136 -            constraints.gridheight = 1;
   1.137 -            constraints.gridwidth = 1;
   1.138 -            constraints.gridx = 0;
   1.139 -            constraints.gridy = row;
   1.140 -            constraints.weightx = 0;
   1.141 -            constraints.weighty = 0;
   1.142 -            constraints.insets = new Insets(0, 0, 0, 8);
   1.143 -            
   1.144 -            add(l, constraints);
   1.145 -            
   1.146 -            JComponent field;
   1.147 -            int val = prefs.getInt(option.preferencesKey, ((Integer)option.defaultValue).intValue());
   1.148 -            if (iopt.step() > 0) {
   1.149 -                val = Math.min(iopt.maxValue(), Math.max(iopt.minValue(), val));
   1.150 -                JSpinner spinner = new JSpinner(
   1.151 -                        new SpinnerNumberModel(val, iopt.minValue(), iopt.maxValue(), iopt.step()));
   1.152 -                spinner.addChangeListener(new ActionListenerImpl(option.preferencesKey, prefs));
   1.153 -                field = spinner;
   1.154 -            } else {
   1.155 -                NumberFormatter nf = new NumberFormatter();
   1.156 -                nf.setValueClass(Integer.class);
   1.157 -                nf.setMaximum(iopt.maxValue());
   1.158 -                nf.setMinimum(iopt.minValue());
   1.159 -                JFormattedTextField formatted = new JFormattedTextField(nf);
   1.160 -                field = formatted;
   1.161 -            }
   1.162 -            if (option.tooltip != null && !option.tooltip.isEmpty()) {
   1.163 -                field.setToolTipText(option.tooltip);
   1.164 -            }
   1.165 -            constraints = new GridBagConstraints();
   1.166 -            constraints.anchor = GridBagConstraints.WEST;
   1.167 -            constraints.fill = GridBagConstraints.HORIZONTAL;
   1.168 -            constraints.gridheight = 1;
   1.169 -            constraints.gridwidth = 1;
   1.170 -            constraints.gridx = 1;
   1.171 -            constraints.gridy = row;
   1.172 -            constraints.weightx = 0;
   1.173 -            constraints.weighty = 0;
   1.174 -            
   1.175 -            add(field, constraints);
   1.176 -
   1.177 -            constraints = new GridBagConstraints();
   1.178 -            constraints.anchor = GridBagConstraints.WEST;
   1.179 -            constraints.fill = GridBagConstraints.HORIZONTAL;
   1.180 -            constraints.gridheight = 1;
   1.181 -            constraints.gridwidth = GridBagConstraints.REMAINDER;
   1.182 -            constraints.gridx = 2;
   1.183 -            constraints.gridy = row++;
   1.184 -            constraints.weightx = 1;
   1.185 -            constraints.weighty = 0;
   1.186 -            
   1.187 -            add(new JPanel(), constraints);
   1.188 -        }
   1.189 -        
   1.190 -        private JComponent createBooleanOption(OptionDescriptor option, Preferences prefs)  {
   1.191 -            JCheckBox checkBox = new JCheckBox();
   1.192 -
   1.193 -            org.openide.awt.Mnemonics.setLocalizedText(checkBox, option.displayName);
   1.194 -            checkBox.setToolTipText(option.tooltip);
   1.195 -            checkBox.addActionListener(new ActionListenerImpl(option.preferencesKey, prefs));
   1.196 -
   1.197 -            checkBox.setSelected(prefs.getBoolean(option.preferencesKey, 
   1.198 -                    Boolean.TRUE == option.defaultValue));
   1.199 -            GridBagConstraints constraints = new GridBagConstraints();
   1.200 -
   1.201 -            constraints.anchor = GridBagConstraints.WEST;
   1.202 -            constraints.fill = GridBagConstraints.NONE;
   1.203 -            constraints.gridheight = 1;
   1.204 -            constraints.gridwidth = 2;
   1.205 -            constraints.gridx = 0;
   1.206 -            constraints.gridy = row++;
   1.207 -            constraints.weightx = 0;
   1.208 -            constraints.weighty = 0;
   1.209 -
   1.210 -            add(checkBox, constraints);
   1.211 -            return checkBox;
   1.212 -        }
   1.213 -                
   1.214 -        
   1.215 -        private static final class ActionListenerImpl implements ActionListener, ChangeListener {
   1.216 -            private final String key;
   1.217 -            private final Preferences prefs;
   1.218 -
   1.219 -            public ActionListenerImpl(String key, Preferences prefs) {
   1.220 -                this.key = key;
   1.221 -                this.prefs = prefs;
   1.222 -            }
   1.223 -
   1.224 -            @Override
   1.225 -            public void actionPerformed(ActionEvent e) {
   1.226 -                JCheckBox checkBox = ((JCheckBox)e.getSource());
   1.227 -                prefs.putBoolean(key, checkBox.isSelected());
   1.228 -            }
   1.229 -
   1.230 -            @Override
   1.231 -            public void stateChanged(ChangeEvent e) {
   1.232 -                Integer i = (Integer)((JSpinner)e.getSource()).getValue();
   1.233 -                prefs.putInt(key, i);
   1.234 -            }
   1.235 -
   1.236 -        }
   1.237 -        
   1.238 -    }
   1.239 -
   1.240 -    public static final class OptionDescriptor {
   1.241 -        public final String preferencesKey;
   1.242 -        public final Object defaultValue;
   1.243 -        public final String displayName;
   1.244 -        public final String tooltip;
   1.245 -        /**
   1.246 -         * The original Annotation object, type-specific parameters.
   1.247 -         */
   1.248 -        public final Object parameters;
   1.249 -
   1.250 -        public OptionDescriptor(
   1.251 -                String preferencesKey, 
   1.252 -                Object defaultValue, 
   1.253 -                String displayName, String tooltip, 
   1.254 -                Object parameters) {
   1.255 -            this.preferencesKey = preferencesKey;
   1.256 -            this.defaultValue = defaultValue;
   1.257 -            this.displayName = displayName;
   1.258 -            this.tooltip = tooltip;
   1.259 -            this.parameters = parameters;
   1.260 -        }
   1.261 -
   1.262 -    }
   1.263 -
   1.264 -}