]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/LogContentProvider.java
IMprove centering of defualt login dialog in RCP
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui / src / main / java / org / argeo / security / ui / views / LogContentProvider.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.security.ui.views;
17
18 import java.text.DateFormat;
19 import java.text.SimpleDateFormat;
20 import java.util.Arrays;
21 import java.util.Date;
22 import java.util.LinkedList;
23 import java.util.List;
24
25 import org.argeo.ArgeoLogListener;
26 import org.eclipse.jface.viewers.ILazyContentProvider;
27 import org.eclipse.jface.viewers.TableViewer;
28 import org.eclipse.jface.viewers.Viewer;
29 import org.eclipse.swt.widgets.Table;
30 import org.eclipse.swt.widgets.TableItem;
31
32 /** A content provider maintaining an array of lines */
33 class LogContentProvider implements ILazyContentProvider, ArgeoLogListener {
34 private DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
35
36 private final Long start;
37 /** current - start = line number. first line is number '1' */
38 private Long current;
39
40 // TODO make it configurable
41 private final Integer maxLineBufferSize = 10 * 1000;
42
43 private final TableViewer viewer;
44 private LinkedList<LogLine> lines;
45
46 public LogContentProvider(TableViewer viewer) {
47 this.viewer = viewer;
48 start = System.currentTimeMillis();
49 lines = new LinkedList<LogLine>();
50 current = start;
51 }
52
53 public synchronized void dispose() {
54 lines.clear();
55 lines = null;
56 }
57
58 @SuppressWarnings("unchecked")
59 public synchronized void inputChanged(Viewer viewer, Object oldInput,
60 Object newInput) {
61 List<String> lin = (List<String>) newInput;
62 if (lin == null)
63 return;
64 for (String line : lin) {
65 addLine(line);
66 }
67 this.viewer.setItemCount(lines.size());
68 }
69
70 public void updateElement(int index) {
71 viewer.replace(lines.get(index), index);
72 }
73
74 public synchronized void appendLog(String username, Long timestamp,
75 String level, String category, String thread, Object msg,
76 String[] exception) {
77 // check if valid
78 if (lines == null)
79 return;
80
81 String message = msg.toString();
82 int count = 0;
83 String prefix = prefix(username, timestamp, level, category, thread)
84 .toString();
85 // String suffix = suffix(username, timestamp, level, category, thread);
86 for (String line : message.split("\n")) {
87 addLine(count == 0 ? prefix + line : line);
88 count++;
89 }
90
91 if (exception != null) {
92 for (String ste : exception) {
93 addLine(ste);
94 }
95 }
96
97 viewer.getTable().getDisplay().asyncExec(new Runnable() {
98 public void run() {
99 if (lines == null)
100 return;
101 viewer.setItemCount(lines.size());
102 // doesn't work with syncExec
103 scrollToLastLine();
104 }
105 });
106 }
107
108 protected StringBuffer prefix(String username, Long timestamp,
109 String level, String category, String thread) {
110 StringBuffer buf = new StringBuffer("");
111 buf.append(dateFormat.format(new Date(timestamp))).append(" ");
112 // buf.append(level).append(" ");
113 return buf;
114 }
115
116 /** Normalize string to the given size */
117 protected String norm(String str, Integer size) {
118 int length = str.length();
119 if (length == size)
120 return str;
121 else if (length > size)
122 return str.substring(0, size);
123 else {
124 char[] arr = new char[size - length];
125 Arrays.fill(arr, ' ');
126 return str + new String(arr);
127 }
128 }
129
130 // protected String suffix(String username, Long timestamp, String level,
131 // String category, String thread) {
132 // return "";
133 // }
134
135 /** Scroll to the last line */
136 protected synchronized void scrollToLastLine() {
137 // we try to show last line with two methods
138 // viewer.reveal(lines.peekLast());
139
140 Table table = viewer.getTable();
141 TableItem ti = table.getItem(table.getItemCount() - 1);
142 table.showItem(ti);
143 }
144
145 protected synchronized LogLine addLine(String line) {
146 // check for maximal size and purge if necessary
147 while (lines.size() >= maxLineBufferSize) {
148 for (int i = 0; i < maxLineBufferSize / 10; i++) {
149 lines.poll();
150 }
151 }
152
153 current++;
154 LogLine logLine = new LogLine(current, line);
155 lines.add(logLine);
156 return logLine;
157 }
158
159 private class LogLine {
160 private Long linenumber;
161 private String message;
162
163 public LogLine(Long linenumber, String message) {
164 this.linenumber = linenumber;
165 this.message = message;
166 }
167
168 @Override
169 public int hashCode() {
170 return linenumber.intValue();
171 }
172
173 @Override
174 public boolean equals(Object obj) {
175 if (obj instanceof LogLine)
176 return ((LogLine) obj).linenumber.equals(linenumber);
177 else
178 return false;
179 }
180
181 @Override
182 public String toString() {
183 return message;
184 }
185
186 }
187 }