summaryrefslogtreecommitdiff
path: root/src/plugins/logger.collector/src/com/motorola/studio/android/logger/collector/ui/LogFileColumn.java
blob: 4541f320181519036e04ad169c917a51a8832aa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed 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.
 */
package com.motorola.studio.android.logger.collector.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.motorola.studio.android.logger.collector.core.ILogFile;
import com.motorola.studio.android.logger.collector.core.internal.CollectLogFile;
import com.motorola.studio.android.logger.collector.util.LoggerCollectorMessages;
import com.motorola.studio.android.logger.collector.util.PlatformException;
import com.motorola.studio.android.logger.collector.util.WidgetsUtil;

/**
 * This class visually represents a log file Table View.
 */
public class LogFileColumn extends Composite
{

    /**
     * The table of log files.
     */
    private Table tableLogFile = null;

    /**
     * 
     */
    CollectLogFile collectLogFile = null;

    /**
     * Public Constructor.
     * 
     * @param parent Parent this Composite.
     * @param style Composite Style.
     */
    public LogFileColumn(Composite parent, int style)
    {
        super(parent, style);
        collectLogFile = new CollectLogFile();
        initialize();
    }

    /**
     * This method contains property of this composite.
     */
    private void initialize()
    {
        GridLayout gridLayoutTableView = new GridLayout();
        gridLayoutTableView.horizontalSpacing = 0;
        gridLayoutTableView.marginWidth = 0;
        gridLayoutTableView.marginHeight = 0;
        gridLayoutTableView.verticalSpacing = 0;
        GridData gridDatagridLayout = new GridData(GridData.FILL_VERTICAL | SWT.TOP);
        this.setBackgroundMode(SWT.NONE);
        this.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
        this.setLayoutData(gridDatagridLayout);
        this.setLayout(gridLayoutTableView);

        GridData gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_BOTH);
        gridData.widthHint = 230;
        gridData.heightHint = 300;
        this.setLayoutData(gridData);

        refresh();
    }

    /**
     * This is a callback that will allow us to create the viewer and initialize
     * it.
     */
    public void refresh()
    {
        GridData gridDataTableView =
                new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_BOTH);
        gridDataTableView.widthHint = 236;
        gridDataTableView.heightHint = 300;
        if (tableLogFile == null)
        {
            tableLogFile = new Table(this, SWT.CHECK | SWT.BORDER);
            tableLogFile.setLayoutData(gridDataTableView);
        }
        tableLogFile.removeAll();
        try
        {
            for (ILogFile log : collectLogFile.getLogFileList())
            {
                TableItem tableItem = new TableItem(tableLogFile, SWT.NONE);
                tableItem.setText(log.getLogName());
                tableItem.setData(log);
            }
        }
        catch (Exception e)
        {
            MessageDialog.openError(
                    getShell(),
                    LoggerCollectorMessages.getInstance().getString(
                            "logger.collector.wizard.page.title"), //$NON-NLS-1$
                    LoggerCollectorMessages.getInstance().getString(
                            "error.logger.collector.mount.tableview")); //$NON-NLS-1$
        }
        packTableColumns();
        checkAll(true);
    }

    /**
     * This method is responsible to pack tree columns
     */
    private void packTableColumns()
    {
        // Pack the columns
        TableColumn[] columns = tableLogFile.getColumns();
        for (int i = 0, n = columns.length; i < n; i++)
        {
            columns[i].pack();
        }
    }

    /**
     * This method collects and zips selected log files.
     * 
     * @param directory The output directory
     * @param filename The output file name
     * @return if collects successfully
     * @throws PlatformException
     */
    public boolean collect(String filename) throws PlatformException
    {
        return new CollectLogFile().zipLogFiles(filename,
                WidgetsUtil.getCheckedLeafItems(this.tableLogFile));
    }

    /**
     * This method tests if the table view has nodes and if there is selected
     * nodes.
     * 
     * @return true if the table view has nodes and if there is selected nodes.
     */
    public boolean hasNodeSelected()
    {
        return WidgetsUtil.getCheckedLeafItems(this.tableLogFile).size() > 0;
    }

    /**
     * This method collects and compacts selected log files.
     * 
     * @param directory The output directory
     * @param filename The output file name
     * @return if collects successfully
     */
    public ArrayList<String> selectedLogFilesExist()
    {
        List<TableItem> list = WidgetsUtil.getCheckedLeafItems(this.tableLogFile);
        ArrayList<String> notFoundItems = new ArrayList<String>();
        for (TableItem tableItem : list)
        {
            Object data = tableItem.getData();
            if (data instanceof ILogFile)
            {
                ILogFile logFile = (ILogFile) data;
                for (IPath path : logFile.getLogFilePath())
                {
                    if (!WidgetsUtil.fileExist(path.toOSString()))
                    {
                        notFoundItems.add(logFile.getLogName());
                    }

                }
            }
        }
        return notFoundItems;
    }

    /**
     * This method adds a TableListener
     * 
     * @param eventType Type of event
     * @param listener Listener
     */
    public void addTableListener(int eventType, Listener listener)
    {
        this.tableLogFile.addListener(eventType, listener);
    }

    public void checkAll(boolean selectionValue)
    {
        for (TableItem item : tableLogFile.getItems())
        {
            item.setChecked(selectionValue);
        }

    }
}