Browse Source

Table model for the channel list dialog

Change-Id: I5a5afd930047e33506b0c4518836f3a7b4454062
Reviewed-on: http://gerrit.dmdirc.com/2210
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.7rc1
Chris Smith 12 years ago
parent
commit
220e7603db

+ 88
- 0
src/com/dmdirc/addons/ui_swing/adapters/ObservableListTableModelAdapter.java View File

@@ -0,0 +1,88 @@
1
+/*
2
+ * Copyright (c) 2006-2011 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.ui_swing.adapters;
24
+
25
+import com.dmdirc.util.ListObserver;
26
+import com.dmdirc.util.ObservableList;
27
+
28
+import javax.swing.table.AbstractTableModel;
29
+
30
+/**
31
+ * Adapts an observable list for use as a table model.
32
+ *
33
+ * @param <T> The type of object in the list
34
+ */
35
+public abstract class ObservableListTableModelAdapter<T> extends AbstractTableModel {
36
+
37
+    /** Serial version UID. */
38
+    private static final long serialVersionUID = 1L;
39
+
40
+    /** The list we're observing. */
41
+    protected final ObservableList<T> list;
42
+
43
+    /**
44
+     * Creates a new {@link ObservableListTableModelAdapter} backed by the
45
+     * given list.
46
+     *
47
+     * @param list The backing list for this model
48
+     */
49
+    public ObservableListTableModelAdapter(final ObservableList<T> list) {
50
+        this.list = list;
51
+        this.list.addListListener(new Listener());
52
+    }
53
+
54
+    /** {@inheritDoc} */
55
+    @Override
56
+    public int getRowCount() {
57
+        return this.list.size();
58
+    }
59
+
60
+    /**
61
+     * List observer which fires the relevant methods to notify this model's
62
+     * listeners.
63
+     */
64
+    private class Listener implements ListObserver {
65
+
66
+        /** {@inheritDoc} */
67
+        @Override
68
+        public void onItemsAdded(final Object source, final int startIndex,
69
+                final int endIndex) {
70
+            fireTableRowsInserted(startIndex, endIndex);
71
+        }
72
+
73
+        /** {@inheritDoc} */
74
+        @Override
75
+        public void onItemsRemoved(final Object source, final int startIndex,
76
+                final int endIndex) {
77
+            fireTableRowsDeleted(startIndex, endIndex);
78
+        }
79
+
80
+        /** {@inheritDoc} */
81
+        @Override
82
+        public void onItemsChanged(final Object source, final int startIndex,
83
+                final int endIndex) {
84
+            fireTableRowsUpdated(startIndex, endIndex);
85
+        }
86
+    }
87
+
88
+}

+ 92
- 0
src/com/dmdirc/addons/ui_swing/dialogs/channellist/ChannelListTableModel.java View File

@@ -0,0 +1,92 @@
1
+/*
2
+ * Copyright (c) 2006-2011 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.ui_swing.dialogs.channellist;
24
+
25
+import com.dmdirc.addons.ui_swing.adapters.ObservableListTableModelAdapter;
26
+import com.dmdirc.lists.GroupListEntry;
27
+import com.dmdirc.lists.GroupListManager;
28
+
29
+/**
30
+ * Table model for channel list results.
31
+ */
32
+public class ChannelListTableModel extends ObservableListTableModelAdapter<GroupListEntry> {
33
+
34
+    /** Serial version UID. */
35
+    private static final long serialVersionUID = 1L;
36
+
37
+    /** The names to use for the model's columns. */
38
+    private static final String[] COLUMN_NAMES = new String[] {
39
+        "Name", "Users", "Topic"
40
+    };
41
+
42
+    /** The types to use for the model's columns. */
43
+    @SuppressWarnings("rawtypes")
44
+    private static final Class[] COLUMN_TYPES = new Class[] {
45
+        String.class, Integer.class, String.class
46
+    };
47
+
48
+    /**
49
+     * Creates a new table model backed by the given manager.
50
+     *
51
+     * @param manager The manager to use to retrieve group list entries.
52
+     */
53
+    public ChannelListTableModel(final GroupListManager manager) {
54
+        super(manager.getGroups());
55
+    }
56
+
57
+    /** {@inheritDoc} */
58
+    @Override
59
+    public int getColumnCount() {
60
+        return 3;
61
+    }
62
+
63
+    /** {@inheritDoc} */
64
+    @Override
65
+    public Object getValueAt(final int rowIndex, final int columnIndex) {
66
+        final GroupListEntry entry = list.get(rowIndex);
67
+
68
+        switch (columnIndex) {
69
+            case 0:
70
+                return entry.getName();
71
+            case 1:
72
+                return entry.getUsers();
73
+            case 2:
74
+                return entry.getTopic();
75
+        }
76
+
77
+        return null;
78
+    }
79
+
80
+    /** {@inheritDoc} */
81
+    @Override
82
+    public Class<?> getColumnClass(final int columnIndex) {
83
+        return COLUMN_TYPES[columnIndex];
84
+    }
85
+
86
+    /** {@inheritDoc} */
87
+    @Override
88
+    public String getColumnName(final int column) {
89
+        return COLUMN_NAMES[column];
90
+    }
91
+
92
+}

Loading…
Cancel
Save