|  | // | 
|  | //     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. | 
|  | // | 
|  |  | 
|  | = DevFaqViewSaveTTVColumns | 
|  | :jbake-type: wiki | 
|  | :jbake-tags: wiki, devfaq, needsreview | 
|  | :jbake-status: published | 
|  | :keywords: Apache NetBeans wiki DevFaqViewSaveTTVColumns | 
|  | :description: Apache NetBeans wiki DevFaqViewSaveTTVColumns | 
|  | :toc: left | 
|  | :toc-title: | 
|  | :syntax: true | 
|  |  | 
|  | == Saving Column Attributes of a TreeTableView | 
|  |  | 
|  | Assuming you are embedding a `TreeTableView` (TTV) inside a `TopComponent`, you can override the `readExternal(ObjectInput)` and `writeExternal(ObjectOutput)` methods for preserving the attributes of the columns in your TTV (e.g. the column ordering, sorted-ness, sorting order, visibility, and width). | 
|  |  | 
|  | Start by keeping a reference to the `Node.Property` array used to define the columns of the TTV, since there is no way to get those properties from the TTV (i.e. there is no `getProperties()` method). The examples below will refer to this `Node.Property` array as "columns". | 
|  |  | 
|  | In order to get and set the column widths of the tree-table, we need to subclass `TreeTableView` and provide a `getTable()` method that returns the `treeTable` protected field of the `TreeTableView` class. In the examples below, this reference will be referred to as `treeTable` for brevity. | 
|  |  | 
|  | First let us save the column attributes to the serialized `TopComponent` via the `writeExternal()` method. | 
|  |  | 
|  | [source,java] | 
|  | ---- | 
|  |  | 
|  | public void writeExternal(ObjectOutput out) throws IOException { | 
|  | super.writeExternal(out); | 
|  | out.writeInt(columns.length); | 
|  | for (int ii = 0; ii < columns.length; ii++) { | 
|  | Boolean b = (Boolean) columns[Ii].getValue("InvisibleInTreeTableView"); | 
|  | if (b == null) { | 
|  | b = Boolean.FALSE; | 
|  | } | 
|  | out.writeBoolean(b.booleanValue()); | 
|  | Integer i = (Integer) columns[Ii].getValue("OrderNumberTTV"); | 
|  | if (i == null) { | 
|  | i = new Integer(ii); | 
|  | } | 
|  | out.writeInt(i.intValue()); | 
|  | b = (Boolean) columns[Ii].getValue("SortingColumnTTV"); | 
|  | if (b == null) { | 
|  | b = Boolean.FALSE; | 
|  | } | 
|  | out.writeBoolean(b.booleanValue()); | 
|  | b = (Boolean) columns[Ii].getValue("DescendingOrderTTV"); | 
|  | if (b == null) { | 
|  | b = Boolean.FALSE; | 
|  | } | 
|  | out.writeBoolean(b.booleanValue()); | 
|  | } | 
|  | try { | 
|  | TableColumnModel tcm = treeTable.getColumnModel(); | 
|  | int count = tcm.getColumnCount(); | 
|  | for (int index = 0; index < count; index++) { | 
|  | TableColumn tc = tcm.getColumn(index); | 
|  | out.writeInt(tc.getWidth()); | 
|  | } | 
|  | } catch (IOException ioe) { | 
|  | ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe); | 
|  | } | 
|  | } | 
|  |  | 
|  | ---- | 
|  |  | 
|  | Next, we see how to deserialize the column attributes. | 
|  |  | 
|  | [source,java] | 
|  | ---- | 
|  |  | 
|  | public void readExternal(ObjectInput in) | 
|  | throws IOException, ClassNotFoundException { | 
|  | super.readExternal(in); | 
|  | try { | 
|  | int count = in.readInt(); | 
|  | for (int ii = 0; ii < count; ii++) { | 
|  | boolean b = in.readBoolean(); | 
|  | columns[Ii].setValue("InvisibleInTreeTableView", Boolean.valueOf(b)); | 
|  | int i = in.readInt(); | 
|  | columns[Ii].setValue("OrderNumberTTV", Integer.valueOf(i)); | 
|  | b = in.readBoolean(); | 
|  | columns[Ii].setValue("SortingColumnTTV", Boolean.valueOf(b)); | 
|  | b = in.readBoolean(); | 
|  | columns[Ii].setValue("DescendingOrderTTV", Boolean.valueOf(b)); | 
|  | } | 
|  | } catch (Exception e) { | 
|  | // Could be reading an old instance which is missing data. | 
|  | // In any case, ignore this as there is no use in reporting it. | 
|  | } | 
|  | nodeView.setProperties(columns); | 
|  |  | 
|  | // Read the column widths from the stream immediately and save them | 
|  | // to a final array for the Runnable below to access them. | 
|  | TableColumnModel tcm = treeTable.getColumnModel(); | 
|  | int count = tcm.getColumnCount(); | 
|  | final int[[ | ]] widths = new int[Count]; | 
|  | try { | 
|  | for (int index = 0; index < count; index++) { | 
|  | widths[Index] = in.readInt(); | 
|  | } | 
|  | } catch (IOException ioe) { | 
|  | // Could be reading an old instance which is missing data. | 
|  | // In any case, ignore this as there is no use in reporting it | 
|  | // (and return immediately so as not to invoke the runnable). | 
|  | return; | 
|  | } | 
|  |  | 
|  | // Changing Swing widgets must be done on the AWT event thread. | 
|  | EventQueue.invokeLater(new Runnable() { | 
|  | public void run() { | 
|  | // TreeTableView prohibits moving the tree | 
|  | // column, so it is always offset zero. | 
|  | setTreePreferredWidth(widths[0]); | 
|  | for (int index = 1; index < widths.length; index++) { | 
|  | setTableColumnPreferredWidth(index - 1, widths[Index]); | 
|  | } | 
|  | } | 
|  | }); | 
|  | } | 
|  |  | 
|  | ---- | 
|  |  | 
|  | == Apache Migration Information | 
|  |  | 
|  | The content in this page was kindly donated by Oracle Corp. to the | 
|  | Apache Software Foundation. | 
|  |  | 
|  | This page was exported from link:http://wiki.netbeans.org/DevFaqViewSaveTTVColumns[http://wiki.netbeans.org/DevFaqViewSaveTTVColumns] , | 
|  | that was last modified by NetBeans user Jtulach | 
|  | on 2010-07-24T19:14:04Z. | 
|  |  | 
|  |  | 
|  | *NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed. |