Commit | Line | Data |
---|---|---|
14665360 PT |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.tmf.ui.editors; | |
14 | ||
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
16 | ||
17 | import java.util.Arrays; | |
18 | ||
19 | import org.eclipse.jface.dialogs.IDialogSettings; | |
20 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; | |
21 | ||
22 | /** | |
23 | * This class manages the column settings associated with a trace type. | |
24 | * | |
25 | * @author Patrick Tasse | |
26 | * @since 1.0 | |
27 | */ | |
28 | public class TmfTraceColumnManager { | |
29 | ||
30 | private static final String ROOT_SECTION_NAME = checkNotNull(TmfTraceColumnManager.class.getSimpleName()); | |
31 | private static final String COLUMN_ORDER_SECTION_NAME = "column.order"; //$NON-NLS-1$ | |
32 | ||
33 | /** | |
34 | * Get the latest saved table column order for the specified trace type. | |
35 | * Returns null if no column order is set. | |
36 | * | |
37 | * @param traceTypeId | |
38 | * the trace type id | |
39 | * @return the table column order, or null | |
40 | */ | |
41 | public static int[] loadColumnOrder(String traceTypeId) { | |
42 | if (traceTypeId == null) { | |
43 | return null; | |
44 | } | |
45 | IDialogSettings settings = Activator.getDefault().getDialogSettings(); | |
46 | IDialogSettings section = settings.getSection(ROOT_SECTION_NAME); | |
47 | if (section == null) { | |
48 | return null; | |
49 | } | |
50 | IDialogSettings columnOrderSection = section.getSection(COLUMN_ORDER_SECTION_NAME); | |
51 | if (columnOrderSection == null) { | |
52 | return null; | |
53 | } | |
54 | String string = columnOrderSection.get(traceTypeId); | |
55 | if (string == null || string.length() < 3) { | |
56 | return null; | |
57 | } | |
58 | if (string.charAt(0) != '[' || string.charAt(string.length() - 1) != ']') { | |
59 | return null; | |
60 | } | |
61 | String[] array = string.substring(1, string.length() - 1).split(", "); //$NON-NLS-1$ | |
62 | int[] columnOrder = new int[array.length]; | |
63 | try { | |
64 | for (int i = 0; i < array.length; i++) { | |
65 | columnOrder[i] = Integer.valueOf(array[i]); | |
66 | } | |
67 | return columnOrder; | |
68 | } catch (NumberFormatException e) { | |
69 | return null; | |
70 | } | |
71 | } | |
72 | ||
73 | /** | |
74 | * Saves the table column order for the specified trace type. Passing a null | |
75 | * column order clears it. | |
76 | * | |
77 | * @param traceTypeId | |
78 | * the trace type id | |
79 | * @param columnOrder | |
80 | * the table column order | |
81 | */ | |
82 | public static void saveColumnOrder(String traceTypeId, int[] columnOrder) { | |
83 | if (traceTypeId == null) { | |
84 | return; | |
85 | } | |
86 | if (columnOrder == null) { | |
87 | clearColumnOrder(traceTypeId); | |
88 | return; | |
89 | } | |
90 | IDialogSettings settings = Activator.getDefault().getDialogSettings(); | |
91 | IDialogSettings section = settings.getSection(ROOT_SECTION_NAME); | |
92 | if (section == null) { | |
93 | section = settings.addNewSection(ROOT_SECTION_NAME); | |
94 | } | |
95 | IDialogSettings columnOrderSection = section.getSection(COLUMN_ORDER_SECTION_NAME); | |
96 | if (columnOrderSection == null) { | |
97 | columnOrderSection = section.addNewSection(COLUMN_ORDER_SECTION_NAME); | |
98 | } | |
99 | columnOrderSection.put(traceTypeId, Arrays.toString(columnOrder)); | |
100 | } | |
101 | ||
102 | /** | |
103 | * Clears the table column order for the specified trace type. | |
104 | * | |
105 | * @param traceTypeId | |
106 | * the trace type id | |
107 | */ | |
108 | public static void clearColumnOrder(String traceTypeId) { | |
109 | if (traceTypeId == null) { | |
110 | return; | |
111 | } | |
112 | IDialogSettings settings = Activator.getDefault().getDialogSettings(); | |
113 | IDialogSettings section = settings.getSection(ROOT_SECTION_NAME); | |
114 | if (section == null) { | |
115 | return; | |
116 | } | |
117 | IDialogSettings columnOrderSection = section.getSection(COLUMN_ORDER_SECTION_NAME); | |
118 | if (columnOrderSection == null) { | |
119 | return; | |
120 | } | |
121 | columnOrderSection.put(traceTypeId, (String) null); | |
122 | } | |
123 | } |