|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright 2014 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
| 7 | + * Other names may be trademarks of their respective owners. |
| 8 | + * |
| 9 | + * The contents of this file are subject to the terms of either the GNU |
| 10 | + * General Public License Version 2 only ("GPL") or the Common |
| 11 | + * Development and Distribution License("CDDL") (collectively, the |
| 12 | + * "License"). You may not use this file except in compliance with the |
| 13 | + * License. You can obtain a copy of the License at |
| 14 | + * http://www.netbeans.org/cddl-gplv2.html |
| 15 | + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
| 16 | + * specific language governing permissions and limitations under the |
| 17 | + * License. When distributing the software, include this License Header |
| 18 | + * Notice in each file and include the License file at |
| 19 | + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
| 20 | + * particular file as subject to the "Classpath" exception as provided |
| 21 | + * by Oracle in the GPL Version 2 section of the License file that |
| 22 | + * accompanied this code. If applicable, add the following below the |
| 23 | + * License Header, with the fields enclosed by brackets [] replaced by |
| 24 | + * your own identifying information: |
| 25 | + * "Portions Copyrighted [year] [name of copyright owner]" |
| 26 | + * |
| 27 | + * If you wish your version of this file to be governed by only the CDDL |
| 28 | + * or only the GPL Version 2, indicate your decision by adding |
| 29 | + * "[Contributor] elects to include this software in this distribution |
| 30 | + * under the [CDDL or GPL Version 2] license." If you do not indicate a |
| 31 | + * single choice of license, a recipient has the option to distribute |
| 32 | + * your version of this file under either the CDDL, the GPL Version 2 or |
| 33 | + * to extend the choice of license to its licensees as provided above. |
| 34 | + * However, if you add GPL Version 2 code and therefore, elected the GPL |
| 35 | + * Version 2 license, then the option applies only if the new code is |
| 36 | + * made subject to such option by the copyright holder. |
| 37 | + * |
| 38 | + * Contributor(s): |
| 39 | + * |
| 40 | + * Portions Copyrighted 2014 Sun Microsystems, Inc. |
| 41 | + */ |
| 42 | +package com.junichi11.netbeans.modules.github.issues.repository.ui; |
| 43 | + |
| 44 | +import java.awt.Component; |
| 45 | +import java.awt.Dialog; |
| 46 | +import java.util.List; |
| 47 | +import javax.swing.DefaultComboBoxModel; |
| 48 | +import javax.swing.DefaultListCellRenderer; |
| 49 | +import javax.swing.JList; |
| 50 | +import javax.swing.ListCellRenderer; |
| 51 | +import org.eclipse.egit.github.core.Repository; |
| 52 | +import org.openide.DialogDescriptor; |
| 53 | +import org.openide.DialogDisplayer; |
| 54 | +import org.openide.util.NbBundle; |
| 55 | + |
| 56 | +/** |
| 57 | + * |
| 58 | + * @author junichi11 |
| 59 | + */ |
| 60 | +public class GitHubRepositoryListPanel extends javax.swing.JPanel { |
| 61 | + |
| 62 | + private static final long serialVersionUID = -6030255526517952671L; |
| 63 | + |
| 64 | + private final DefaultComboBoxModel<Repository> repositoryComboBoxModel = new DefaultComboBoxModel<>(); |
| 65 | + |
| 66 | + /** |
| 67 | + * Creates new form GitHubRepositoryListPanel |
| 68 | + */ |
| 69 | + private GitHubRepositoryListPanel(List<Repository> repositories) { |
| 70 | + initComponents(); |
| 71 | + init(repositories); |
| 72 | + } |
| 73 | + |
| 74 | + private void init(List<Repository> repositories) { |
| 75 | + repositoryComboBoxModel.removeAllElements(); |
| 76 | + for (Repository repository : repositories) { |
| 77 | + repositoryComboBoxModel.addElement(repository); |
| 78 | + } |
| 79 | + repositoryComboBox.setRenderer(new RepositoryListCellRenderer(repositoryComboBox.getRenderer())); |
| 80 | + repositoryComboBox.setModel(repositoryComboBoxModel); |
| 81 | + } |
| 82 | + |
| 83 | + @NbBundle.Messages({ |
| 84 | + "GitHubRepositoryListPanel.title=Repositories" |
| 85 | + }) |
| 86 | + public static Repository showDialog(List<Repository> repositories) { |
| 87 | + GitHubRepositoryListPanel panel = new GitHubRepositoryListPanel(repositories); |
| 88 | + DialogDescriptor dialogDescriptor = new DialogDescriptor(panel, Bundle.GitHubRepositoryListPanel_title(), true, DialogDescriptor.OK_CANCEL_OPTION, null, null); |
| 89 | + Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor); |
| 90 | + dialog.pack(); |
| 91 | + dialog.setVisible(true); |
| 92 | + dialog.dispose(); |
| 93 | + if (dialogDescriptor.getValue() == DialogDescriptor.OK_OPTION) { |
| 94 | + return panel.getSelectedRepository(); |
| 95 | + } |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + private Repository getSelectedRepository() { |
| 100 | + return (Repository) repositoryComboBox.getSelectedItem(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * This method is called from within the constructor to initialize the form. |
| 105 | + * WARNING: Do NOT modify this code. The content of this method is always |
| 106 | + * regenerated by the Form Editor. |
| 107 | + */ |
| 108 | + @SuppressWarnings("unchecked") |
| 109 | + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
| 110 | + private void initComponents() { |
| 111 | + |
| 112 | + repositoryComboBox = new javax.swing.JComboBox<Repository>(); |
| 113 | + |
| 114 | + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); |
| 115 | + this.setLayout(layout); |
| 116 | + layout.setHorizontalGroup( |
| 117 | + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 118 | + .addGroup(layout.createSequentialGroup() |
| 119 | + .addContainerGap() |
| 120 | + .addComponent(repositoryComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) |
| 121 | + .addContainerGap()) |
| 122 | + ); |
| 123 | + layout.setVerticalGroup( |
| 124 | + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 125 | + .addGroup(layout.createSequentialGroup() |
| 126 | + .addContainerGap() |
| 127 | + .addComponent(repositoryComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) |
| 128 | + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) |
| 129 | + ); |
| 130 | + }// </editor-fold>//GEN-END:initComponents |
| 131 | + |
| 132 | + // Variables declaration - do not modify//GEN-BEGIN:variables |
| 133 | + private javax.swing.JComboBox<Repository> repositoryComboBox; |
| 134 | + // End of variables declaration//GEN-END:variables |
| 135 | + |
| 136 | + private static class RepositoryListCellRenderer extends DefaultListCellRenderer { |
| 137 | + |
| 138 | + private static final long serialVersionUID = -8530867911076509797L; |
| 139 | + |
| 140 | + private final ListCellRenderer renderer; |
| 141 | + |
| 142 | + public RepositoryListCellRenderer(ListCellRenderer renderer) { |
| 143 | + this.renderer = renderer; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
| 148 | + String text = " "; // NOI18N |
| 149 | + if (value instanceof Repository) { |
| 150 | + Repository repository = (Repository) value; |
| 151 | + text = repository.getName(); |
| 152 | + } |
| 153 | + return renderer.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus); |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | +} |
0 commit comments