PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/mercurial/src/org/netbeans/modules/mercurial/options/PropertiesTable.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 216 lines | 141 code | 26 blank | 49 comment | 19 complexity | fedf5ec132a01f43391bb99c42dbb9a6 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 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. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2009 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.mercurial.options;
  45. import java.awt.Component;
  46. import java.awt.Dimension;
  47. import java.util.Arrays;
  48. import javax.swing.JComponent;
  49. import javax.swing.JLabel;
  50. import javax.swing.JScrollPane;
  51. import javax.swing.JTable;
  52. import javax.swing.event.AncestorEvent;
  53. import javax.swing.event.AncestorListener;
  54. import javax.swing.event.TableModelEvent;
  55. import javax.swing.event.TableModelListener;
  56. import javax.swing.table.DefaultTableCellRenderer;
  57. import javax.swing.table.TableColumnModel;
  58. import javax.swing.table.TableModel;
  59. import org.netbeans.modules.versioning.util.TableSorter;
  60. import org.netbeans.modules.mercurial.ui.properties.HgPropertiesNode;
  61. import org.netbeans.modules.mercurial.options.PropertiesTableModel;
  62. import org.netbeans.modules.versioning.util.SortedTable;
  63. import org.openide.util.NbBundle;
  64. /**
  65. *
  66. * @author Peter Pis
  67. */
  68. public class PropertiesTable implements AncestorListener, TableModelListener {
  69. static public final String[] PROPERTIES_COLUMNS = new String[] {PropertiesTableModel.COLUMN_NAME_NAME, PropertiesTableModel.COLUMN_NAME_VALUE};
  70. private PropertiesTableModel tableModel;
  71. private JTable table;
  72. private TableSorter sorter;
  73. private JComponent component;
  74. private String[] columns;
  75. private String[] sortByColumns;
  76. /** Creates a new instance of PropertiesTable */
  77. public PropertiesTable(JLabel label, String[] columns, String[] sortByColumns) {
  78. init(label, columns, null);
  79. this.sortByColumns = sortByColumns;
  80. setSortingStatus();
  81. }
  82. public PropertiesTable(JLabel label, String[] columns, TableSorter sorter) {
  83. init(label, columns, sorter);
  84. }
  85. private void init(JLabel label, String[] columns, TableSorter sorter) {
  86. tableModel = new PropertiesTableModel(columns);
  87. tableModel.addTableModelListener(this);
  88. if(sorter == null) {
  89. sorter = new TableSorter(tableModel);
  90. }
  91. this.sorter = sorter;
  92. table = new SortedTable(this.sorter);
  93. table.getTableHeader().setReorderingAllowed(false);
  94. table.setDefaultRenderer(String.class, new PropertiesTableCellRenderer());
  95. //table.setDefaultEditor(CommitOptions.class, new CommitOptionsCellEditor());
  96. table.getTableHeader().setReorderingAllowed(true);
  97. table.setRowHeight(table.getRowHeight());
  98. table.addAncestorListener(this);
  99. component = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  100. component.setPreferredSize(new Dimension(340, 150));
  101. table.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PropertiesTable.class, "ACSD_PropertiesTable")); // NOI18N
  102. label.setLabelFor(table);
  103. setColumns(columns);
  104. }
  105. public void setColumns(String[] clmns) {
  106. if (Arrays.equals(columns, clmns))
  107. return;
  108. columns = clmns;
  109. tableModel.setColumns(clmns);
  110. setDefaultColumnSize();
  111. }
  112. public JTable getTable() {
  113. return table;
  114. }
  115. private void setDefaultColumnSize() {
  116. int width = table.getWidth();
  117. TableColumnModel columnModel = table.getColumnModel();
  118. if (columns == null || columnModel == null)
  119. return;
  120. if (columnModel.getColumnCount() != columns.length)
  121. return;
  122. for (int i = 0; i < columns.length; i++) {
  123. String col = columns[i];
  124. sorter.setColumnComparator(i, null);
  125. if (col.equals(PropertiesTableModel.COLUMN_NAME_NAME)) {
  126. columnModel.getColumn(i).setPreferredWidth(width * 20 / 100);
  127. } else if (col.equals(PropertiesTableModel.COLUMN_NAME_VALUE)) {
  128. columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
  129. }
  130. }
  131. }
  132. private void setSortingStatus() {
  133. for (int i = 0; i < sortByColumns.length; i++) {
  134. String sortByColumn = sortByColumns[i];
  135. for (int j = 0; j < columns.length; j++) {
  136. String column = columns[j];
  137. if(column.equals(sortByColumn)) {
  138. sorter.setSortingStatus(j, column.equals(sortByColumn) ? TableSorter.ASCENDING : TableSorter.NOT_SORTED);
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. TableModel getTableModel() {
  145. return tableModel;
  146. }
  147. void dataChanged() {
  148. int idx = table.getSelectedRow();
  149. tableModel.fireTableDataChanged();
  150. if (idx != -1) {
  151. table.getSelectionModel().addSelectionInterval(idx, idx);
  152. }
  153. }
  154. public int getModelIndex(int viewIndex) {
  155. return sorter.modelIndex(viewIndex);
  156. }
  157. public int[] getSelectedItems() {
  158. return table.getSelectedRows();
  159. }
  160. public HgPropertiesNode[] getNodes() {
  161. return tableModel.getNodes();
  162. }
  163. public void setNodes(HgPropertiesNode[] nodes) {
  164. tableModel.setNodes(nodes);
  165. }
  166. public JComponent getComponent() {
  167. return component;
  168. }
  169. public void ancestorAdded(AncestorEvent arg0) {
  170. setDefaultColumnSize();
  171. }
  172. public void ancestorRemoved(AncestorEvent arg0) {
  173. }
  174. public void ancestorMoved(AncestorEvent arg0) {
  175. }
  176. public void tableChanged(TableModelEvent event) {
  177. table.repaint();
  178. }
  179. public class PropertiesTableCellRenderer extends DefaultTableCellRenderer {
  180. public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int rowIndex, int columnIndex) {
  181. Component renderer = super.getTableCellRendererComponent(table, value, hasFocus, hasFocus, rowIndex, columnIndex);
  182. if (renderer instanceof JComponent) {
  183. String strValue = tableModel.getNode(sorter.modelIndex(rowIndex)).getValue();
  184. ((JComponent) renderer).setToolTipText(strValue);
  185. }
  186. setToolTipText(value.toString());
  187. return renderer;
  188. }
  189. }
  190. }