From: <pat...@us...> - 2010-07-01 20:13:29
|
Revision: 1079 http://cishell.svn.sourceforge.net/cishell/?rev=1079&view=rev Author: pataphil Date: 2010-07-01 20:13:22 +0000 (Thu, 01 Jul 2010) Log Message: ----------- * Refactored some convenience styled printing functionality out of org.cishell.reference.gui.log.LogView and into org.cishell.utilities.swt. * Discussed with Micah. Modified Paths: -------------- trunk/clients/gui/org.cishell.reference.gui.log/META-INF/MANIFEST.MF trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java Modified: trunk/clients/gui/org.cishell.reference.gui.log/META-INF/MANIFEST.MF =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.log/META-INF/MANIFEST.MF 2010-07-01 20:11:42 UTC (rev 1078) +++ trunk/clients/gui/org.cishell.reference.gui.log/META-INF/MANIFEST.MF 2010-07-01 20:13:22 UTC (rev 1079) @@ -8,4 +8,5 @@ Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime Eclipse-LazyStart: true -Import-Package: org.osgi.service.log;version="1.3.0" +Import-Package: org.cishell.utilities.swt, + org.osgi.service.log;version="1.3.0" Modified: trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java 2010-07-01 20:11:42 UTC (rev 1078) +++ trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java 2010-07-01 20:13:22 UTC (rev 1079) @@ -14,32 +14,27 @@ * ***************************************************************************/ package org.cishell.reference.gui.log; -//standard java import java.io.BufferedWriter; -import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.net.URL; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties; +import org.cishell.utilities.swt.SWTUtilities; +import org.cishell.utilities.swt.URLClickedListener; +import org.cishell.utilities.swt.URLMouseCursorListener; import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.StyleRange; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.dnd.Clipboard; import org.eclipse.swt.dnd.TextTransfer; import org.eclipse.swt.dnd.Transfer; -import org.eclipse.swt.events.MouseAdapter; -import org.eclipse.swt.events.MouseEvent; -import org.eclipse.swt.events.MouseMoveListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.Cursor; -import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.program.Program; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; @@ -59,45 +54,43 @@ public static final String CONFIGURATION_DIRECTORY = "configuration"; public static final String WELCOME_TEXT_FILE_NAME = "Welcome.properties"; public static final String GREETING_PROPERTY = "greeting"; + + public static final Color URL_COLOR = getSystemColor(SWT.COLOR_BLUE); + public static final Color LOG_ERROR_COLOR = getSystemColor(SWT.COLOR_RED); + public static final Color LOG_WARNING_COLOR = new Color(Display.getDefault(), 255, 127, 0); + public static final Color LOG_INFO_COLOR = getSystemColor(SWT.COLOR_BLACK); + public static final Color LOG_DEBUG_COLOR = new Color(Display.getDefault(), 150, 150, 150); + + private static Color getSystemColor(final int swtColor) { + final Color[] color = new Color[1]; + + Display.getDefault().syncExec(new Runnable() { + public void run() { + color[0] = Display.getDefault().getSystemColor(swtColor); + } + }); + + return color[0]; + } - private static LogView defaultView; - private static Composite parent; - private static StyledText text; - - private static Map colorMapping; - private static Color URL_COLOR; - private static Color LOG_ERROR_COLOR; - private static Color LOG_WARNING_COLOR; - //FOR ALGORITHM INFO - private static Color LOG_INFO_COLOR; - //FOR ACTIVITY INFO - private static Color LOG_DEBUG_COLOR; - - private static URLClickedListener urlListener; - private static URLMouseCursorListener urlCursorListener; + public static final Map<String, Color> COLOR_MAPPING = getColorMapping(); + + private static Map<String, Color> getColorMapping() { + Map<String, Color> colorMapping = new HashMap<String, Color>(); + colorMapping.put("" + LogService.LOG_DEBUG, LOG_DEBUG_COLOR); + colorMapping.put("" + LogService.LOG_INFO, LOG_INFO_COLOR); + colorMapping.put("" + LogService.LOG_WARNING, LOG_WARNING_COLOR); + colorMapping.put("" + LogService.LOG_ERROR, LOG_ERROR_COLOR); + + return Collections.unmodifiableMap(colorMapping); + } + + private Composite parent; + private StyledText textField; + private URLClickedListener urlListener; + private URLMouseCursorListener urlCursorListener; - static { - Display.getDefault().syncExec(new Runnable(){ - public void run(){ - LOG_ERROR_COLOR = - Display.getDefault().getSystemColor(SWT.COLOR_RED); - // Orange. - LOG_WARNING_COLOR = - new Color(Display.getDefault(), 255, 127, 0); - LOG_INFO_COLOR = - Display.getDefault().getSystemColor(SWT.COLOR_BLACK); - // Gray. - LOG_DEBUG_COLOR = - new Color(Display.getDefault(), 150, 150, 150); - - URL_COLOR = Display.getDefault().getSystemColor(SWT.COLOR_BLUE); - } - }); - } - public LogView() { - defaultView = this; - //TODO: Need to set the log level based on preferences service /* Configuration cfg = IVC.getInstance().getConfiguration(); boolean showAll = cfg.getBoolean(IVCPreferences.SHOW_ALL_ERRORS_PREFERENCE); @@ -109,66 +102,56 @@ currentLevel = LogService.LOG_INFO; } */ - this.colorMapping = new HashMap(); - this.colorMapping.put("" + LogService.LOG_DEBUG, LOG_DEBUG_COLOR); - this.colorMapping.put("" + LogService.LOG_INFO, LOG_INFO_COLOR); - this.colorMapping.put("" + LogService.LOG_WARNING, LOG_WARNING_COLOR); - this.colorMapping.put("" + LogService.LOG_ERROR, LOG_ERROR_COLOR); } - public static LogView getDefault() { - return defaultView; - } - /** * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite) */ + // TODO: Refactor this. Do we even need the member variables? + @SuppressWarnings("unchecked") public void createPartControl(Composite parent) { - LogView.parent = parent; - this.text = new StyledText(parent, - SWT.BORDER | SWT.V_SCROLL | SWT.WRAP | SWT.READ_ONLY); - this.text.setEditable(false); - this.text.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE)); - this.text.getCaret().setVisible(false); + this.parent = parent; + this.textField = new StyledText(parent, SWT.BORDER | SWT.V_SCROLL | SWT.WRAP | SWT.READ_ONLY); + this.textField.setEditable(false); + this.textField.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE)); + this.textField.getCaret().setVisible(false); // Handle URL. - this.urlListener = new URLClickedListener(); - this.text.addMouseListener(this.urlListener); - this.urlCursorListener = new URLMouseCursorListener(); - this.text.addMouseMoveListener(this.urlCursorListener); + this.urlListener = new URLClickedListener(textField); + this.textField.addMouseListener(this.urlListener); + this.urlCursorListener = new URLMouseCursorListener(this.parent, this.textField); + this.textField.addMouseMoveListener(this.urlCursorListener); - //add copy context menu when hover a block of text and right click the mouse + // Add copy context menu when hover a block of textField and right click the mouse. Display display = Display.getDefault(); - final Clipboard cb = new Clipboard(display); - final Menu menu = new Menu(text); + final Clipboard clipboard = new Clipboard(display); + final Menu menu = new Menu(textField); menu.setVisible(false); MenuItem actionItem = new MenuItem(menu, SWT.PUSH); actionItem.setText("Copy"); - actionItem.addListener(SWT.Selection, - new Listener() { - public void handleEvent(Event event) { - String textData = text.getSelectionText(); - TextTransfer textTransfer = TextTransfer.getInstance(); - cb.setContents(new Object[] { textData }, - new Transfer[] { textTransfer }); - } - }); + actionItem.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event event) { + String textData = LogView.this.textField.getSelectionText(); + TextTransfer textTransfer = TextTransfer.getInstance(); + clipboard.setContents(new Object[] { textData }, new Transfer[] { textTransfer }); + } + }); - text.addSelectionListener(new SelectionAdapter() { - public void widgetSelected(SelectionEvent e) { - String selection = ((StyledText) e.widget).getSelectionText(); + textField.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + String selection = ((StyledText) event.widget).getSelectionText(); if (selection.equals("")) { - text.setMenu(null); + textField.setMenu(null); } else { - text.setMenu(menu); + textField.setMenu(menu); } } }); - //Get LogReaderService through BundleContext - //Add itself to the LogReaderService as a LogListener + // Get LogReaderService through BundleContext. + // Add itself to the LogReaderService as a LogListener. BundleContext context = Activator.getContext(); ServiceReference logReaderServiceReference = context.getServiceReference(LogReaderService.class.getName()); @@ -191,31 +174,27 @@ ServiceReference logServiceReference = context.getServiceReference(LogService.class.getName()); - LogService logService = - (LogService)context.getService(logServiceReference); + LogService logService = (LogService) context.getService(logServiceReference); if (logService != null) { try { - URL welcomeTextFileURL = new URL(new URL(System.getProperty("osgi.configuration.area")), WELCOME_TEXT_FILE_NAME); + URL welcomeTextFileURL = new URL(new URL( + System.getProperty("osgi.configuration.area")), WELCOME_TEXT_FILE_NAME); Properties properties = new Properties(); properties.load(welcomeTextFileURL.openStream()); - String greetingText = - properties.getProperty(GREETING_PROPERTY, null); + String greetingText = properties.getProperty(GREETING_PROPERTY, null); logService.log(LogService.LOG_INFO, greetingText); - } catch (IOException ioException) { - System.err.println("Error reading Welcome properties file: " + - ioException.getMessage()); + } catch (IOException e) { + System.err.println("Error reading Welcome properties file: " + e.getMessage()); } - } - else { + } else { try { FileWriter fstream = new FileWriter("WelcomeTextError.txt", true); BufferedWriter out = new BufferedWriter(fstream); out.write("The Log Service cannot be found.\r\n"); out.close(); - } catch (Exception exception) { - System.err.println("Error writing to file: " + - exception.getMessage()); + } catch (Exception e) { + System.err.println("Error writing to file: " + e.getMessage()); } } } @@ -224,21 +203,27 @@ * @see org.eclipse.ui.part.WorkbenchPart#setFocus() */ public void setFocus() { - text.setFocus(); + textField.setFocus(); } public void logged(final LogEntry entry) { PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() { public void run() { - String message = entry.getMessage(); try { + String message = entry.getMessage(); if (goodMessage(message)) { - //not all messages end w/ a new line, but they - //need to to print properly - if (!message.endsWith("\n")) + // Not all messages end w/ a new line, but they need to to print properly. + if (!message.endsWith("\n")) { message += "\n"; + } - appendString(message, (Color) colorMapping.get(""+entry.getLevel())); + SWTUtilities.appendStringWithURL( + LogView.this.textField, + LogView.this.urlListener, + LogView.this.urlCursorListener, + message, + COLOR_MAPPING.get("" + entry.getLevel()), + URL_COLOR); } } catch (Throwable e) { e.printStackTrace(); @@ -257,173 +242,4 @@ return true; } } - - /* - * append the given string to the console with the given color, - * this will do the job of checking for URLs within the string and - * registering the proper listeners on them as well. - */ - private void appendString(String message, Color color) { - int index = message.indexOf("http://"); - if (index == -1) { - index = message.indexOf("https://"); - } - if (index == -1) { - index = message.indexOf("www."); - } - - if (index > -1) { - String url = message.substring(index); - if (url.indexOf(") ") > -1) { - url = url.substring(0, url.indexOf(") ")); - } - else if (url.indexOf(" ") > -1) { - url = url.substring(0, url.indexOf(" ")); - if (url.trim().endsWith(".") ){ - url=url.substring(0, url.length()-1); - } - } - if (url.endsWith(".\n") || url.endsWith(".\t")){ - url=url.substring(0, url.length()-2); - } - if (url.indexOf("\n") > -1) { - url = url.substring(0, url.indexOf("\n")); - } - if (url.indexOf("\t") > -1) { - url = url.substring(0, url.indexOf("\n")); - } - - - printHelper(message.substring(0, index), color, SWT.NORMAL); - urlListener.addUrl(text.getText().length(), url); - urlCursorListener.addUrl(text.getText().length(), url); - printHelper(url, URL_COLOR, SWT.BOLD); - appendString(message.substring(index + url.length()), color); - } else { - printHelper(message, color, SWT.NORMAL); - } - } - - /* - * helper to actually format the string with a style range and - * append it to the StyledText control - */ - private static void printHelper(final String inText, final Color color, final int style) { - Display.getDefault().syncExec(new Runnable(){ - public void run(){ - if (!text.isDisposed()) { - text.append(inText); - - StyleRange sr = new StyleRange(); - sr.start = text.getText().length() - inText.length(); - sr.length = inText.length(); - sr.foreground = color; - sr.fontStyle = style; - text.setStyleRange(sr); - - //autoscroll - text.setTopIndex(text.getLineCount()); - } - } - }); - } - - - /* - * class that monitors the mouse and changes the cursor when it is - * over a URL - */ - private class URLMouseCursorListener implements MouseMoveListener { - Map offsetToUrlMap = new HashMap(); - - public void addUrl(int offset, String url) { - offsetToUrlMap.put(new Integer(offset), url); - } - - public void mouseMove(MouseEvent e) { - int position = -1; - - try { - position = text.getOffsetAtLocation(new Point(e.x, e.y)); - } catch (IllegalArgumentException ex) { - Cursor cursor = new Cursor(parent.getDisplay(), SWT.CURSOR_ARROW); - text.setCursor(cursor); - } - - if (position < 0) { - return; - } - - Integer[] offsets = new Integer[1]; - offsets = (Integer[]) offsetToUrlMap.keySet().toArray(offsets); - - boolean overURL = false; - - for (int i = 0; i < offsets.length; i++) { - Integer offset = offsets[i]; - String url = (String) offsetToUrlMap.get(offset); - - if (offset != null && url != null && (position >= offset.intValue()) && - (position <= (offset.intValue() + url.length()))) { - overURL = true; - - break; - } - } - - if (overURL) { - Cursor cursor = new Cursor(parent.getDisplay(), SWT.CURSOR_HAND); - text.setCursor(cursor); - } else { - Cursor cursor = new Cursor(parent.getDisplay(), SWT.CURSOR_ARROW); - text.setCursor(cursor); - } - } - } - - /* - * class that listens for clicks on urls and launches a browser appropriatly - */ - private class URLClickedListener extends MouseAdapter { - Map offsetToUrlMap = new HashMap(); - - public void addUrl(int offset, String url) { - offsetToUrlMap.put(new Integer(offset), url); - } - - public void mouseDown(MouseEvent e) { - if (e.button != 1) { - return; - } - - int clicked = -1; - - try { - clicked = text.getOffsetAtLocation(new Point(e.x, e.y)); - } catch (IllegalArgumentException ex) { - } - - if (clicked < 0) { - return; - } - - Integer[] offsets = new Integer[1]; - offsets = (Integer[]) offsetToUrlMap.keySet().toArray(offsets); - - for (int i = 0; i < offsets.length; i++) { - Integer offset = offsets[i]; - String url = (String) offsetToUrlMap.get(offset); - - if (url != null && (clicked >= offset.intValue()) && - (clicked <= (offset.intValue() + url.length()))) { - try { - Program.launch(url); - } catch (Exception e1) { - } - } - } - } - } - - } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pat...@us...> - 2010-08-05 19:07:30
|
Revision: 1114 http://cishell.svn.sourceforge.net/cishell/?rev=1114&view=rev Author: pataphil Date: 2010-08-05 19:07:24 +0000 (Thu, 05 Aug 2010) Log Message: ----------- * Changed org.cishell.utilities.swt to org.cishell.utility.swt. Modified Paths: -------------- trunk/clients/gui/org.cishell.reference.gui.log/META-INF/MANIFEST.MF trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogToFile.java trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java Modified: trunk/clients/gui/org.cishell.reference.gui.log/META-INF/MANIFEST.MF =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.log/META-INF/MANIFEST.MF 2010-08-05 19:06:58 UTC (rev 1113) +++ trunk/clients/gui/org.cishell.reference.gui.log/META-INF/MANIFEST.MF 2010-08-05 19:07:24 UTC (rev 1114) @@ -8,5 +8,5 @@ Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime Eclipse-LazyStart: true -Import-Package: org.cishell.utilities.swt, +Import-Package: org.cishell.utility.swt, org.osgi.service.log;version="1.3.0" Modified: trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogToFile.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogToFile.java 2010-08-05 19:06:58 UTC (rev 1113) +++ trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogToFile.java 2010-08-05 19:07:24 UTC (rev 1114) @@ -2,8 +2,6 @@ import java.io.File; import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringWriter; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.logging.FileHandler; Modified: trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java 2010-08-05 19:06:58 UTC (rev 1113) +++ trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java 2010-08-05 19:07:24 UTC (rev 1114) @@ -24,9 +24,9 @@ import java.util.Map; import java.util.Properties; -import org.cishell.utilities.swt.SWTUtilities; -import org.cishell.utilities.swt.URLClickedListener; -import org.cishell.utilities.swt.URLMouseCursorListener; +import org.cishell.utility.swt.SWTUtilities; +import org.cishell.utility.swt.URLClickedListener; +import org.cishell.utility.swt.URLMouseCursorListener; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.dnd.Clipboard; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pat...@us...> - 2011-02-11 13:37:28
|
Revision: 1204 http://cishell.svn.sourceforge.net/cishell/?rev=1204&view=rev Author: pataphil Date: 2011-02-11 13:37:21 +0000 (Fri, 11 Feb 2011) Log Message: ----------- * Highlighting text on the Console depending on what Data is selected on the Data Manager is in the works. * Code cleanup. * Not reviewed. Modified Paths: -------------- trunk/clients/gui/org.cishell.reference.gui.log/META-INF/MANIFEST.MF trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/Activator.java trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java Modified: trunk/clients/gui/org.cishell.reference.gui.log/META-INF/MANIFEST.MF =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.log/META-INF/MANIFEST.MF 2011-02-11 13:26:16 UTC (rev 1203) +++ trunk/clients/gui/org.cishell.reference.gui.log/META-INF/MANIFEST.MF 2011-02-11 13:37:21 UTC (rev 1204) @@ -8,5 +8,9 @@ Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime Eclipse-LazyStart: true -Import-Package: org.cishell.utility.swt, +Import-Package: com.google.common.collect, + org.cishell.app.service.datamanager;version="1.0.0", + org.cishell.framework;version="1.0.0", + org.cishell.framework.data;version="1.0.0", + org.cishell.utility.swt, org.osgi.service.log;version="1.3.0" Modified: trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/Activator.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/Activator.java 2011-02-11 13:26:16 UTC (rev 1203) +++ trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/Activator.java 2011-02-11 13:37:21 UTC (rev 1204) @@ -1,5 +1,6 @@ package org.cishell.reference.gui.log; +import org.cishell.app.service.datamanager.DataManagerService; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; @@ -13,24 +14,29 @@ public static final String PLUGIN_ID = "org.cishell.reference.gui.log"; private static Activator plugin; private static BundleContext context; + public static DataManagerService dataManager; public Activator() { plugin = this; } - public void start(BundleContext context) throws Exception { - super.start(context); - Activator.context = context; + public void start(BundleContext bundleContext) throws Exception { + super.start(bundleContext); + Activator.context = bundleContext; LogListener fileLogListener = new LogToFile(); ServiceReference serviceReference = - context.getServiceReference(LogReaderService.class.getName()); + bundleContext.getServiceReference(LogReaderService.class.getName()); LogReaderService logReaderService = - (LogReaderService) context.getService(serviceReference); + (LogReaderService) bundleContext.getService(serviceReference); if (logReaderService != null) { logReaderService.addLogListener(fileLogListener); } + + Activator.dataManager = (DataManagerService) + bundleContext.getService(bundleContext.getServiceReference( + DataManagerService.class.getName())); } public void stop(BundleContext bundleContext) throws Exception { Modified: trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java 2011-02-11 13:26:16 UTC (rev 1203) +++ trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java 2011-02-11 13:37:21 UTC (rev 1204) @@ -18,16 +18,23 @@ import java.io.FileWriter; import java.io.IOException; import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Properties; +import org.cishell.app.service.datamanager.DataManagerListener; +import org.cishell.framework.data.Data; +import org.cishell.framework.data.DataProperty; import org.cishell.utility.swt.SWTUtilities; import org.cishell.utility.swt.URLClickedListener; import org.cishell.utility.swt.URLMouseCursorListener; import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyleRange; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.dnd.Clipboard; import org.eclipse.swt.dnd.TextTransfer; @@ -50,7 +57,10 @@ import org.osgi.service.log.LogReaderService; import org.osgi.service.log.LogService; -public class LogView extends ViewPart implements LogListener { +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; + +public class LogView extends ViewPart implements DataManagerListener, LogListener { public static final String CONFIGURATION_DIRECTORY = "configuration"; public static final String WELCOME_TEXT_FILE_NAME = "Welcome.properties"; public static final String GREETING_PROPERTY = "greeting"; @@ -60,7 +70,91 @@ public static final Color LOG_WARNING_COLOR = new Color(Display.getDefault(), 255, 127, 0); public static final Color LOG_INFO_COLOR = getSystemColor(SWT.COLOR_BLACK); public static final Color LOG_DEBUG_COLOR = new Color(Display.getDefault(), 150, 150, 150); + public static final Color UNHIGHLIGHED_BACKGROUND_COLOR = + new Color(Display.getDefault(), 255, 255, 255); + public static final Color HIGHLIGHTED_BACKGROUND_COLOR = + new Color(Display.getDefault(), 200, 200, 200); + public static final boolean HIGHLIGHT_TEXT = false; + + private Multimap<ServiceReference, Bounds> boundsByServiceReference = HashMultimap.create(); + private Collection<StyleRange> nonHighlightStyleRanges = new HashSet<StyleRange>(); + + // DataManagerListener + + public void dataAdded(Data data, String label) {} + public void dataLabelChanged(Data data, String label) {} + public void dataRemoved(Data data) {} + + public void dataSelected(final Data[] data) { + if (HIGHLIGHT_TEXT) { + PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() { + public void run() { + LogView.this.textField.replaceStyleRanges( + 0, LogView.this.textField.getText().length(), new StyleRange[0]); + } + }); + + final Collection<StyleRange> highlights = new ArrayList<StyleRange>(); + + for (Data datum : data) { + Object serviceReferenceObject = + datum.getMetadata().get(DataProperty.SERVICE_REFERENCE); + + if (serviceReferenceObject != null) { + ServiceReference serviceReference = (ServiceReference) serviceReferenceObject; + + if (LogView.this.boundsByServiceReference.containsKey(serviceReference)) { + Collection<Bounds> highlightBounds = + LogView.this.boundsByServiceReference.get(serviceReference); + + for (Bounds highlightBound : highlightBounds) { + StyleRange highlightStyle = new StyleRange(); + highlightStyle.start = highlightBound.start; + highlightStyle.length = highlightBound.length; + highlightStyle.background = HIGHLIGHTED_BACKGROUND_COLOR; + highlights.add(highlightStyle); + } + } + } + } + + final Collection<StyleRange> nonHighlightStyles = new ArrayList<StyleRange>(); + + for (StyleRange nonHighlightStyle : this.nonHighlightStyleRanges) { + StyleRange newStyle = nonHighlightStyle; + + for (StyleRange highlightStyle : highlights) { + int start = nonHighlightStyle.start; + + if ((start >= highlightStyle.start) && + (start < (highlightStyle.start + highlightStyle.length))) { + newStyle = (StyleRange) newStyle.clone(); + newStyle.background = HIGHLIGHTED_BACKGROUND_COLOR; + } + } + + nonHighlightStyles.add(newStyle); + } + + PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() { + public void run() { + try { + for (StyleRange style : highlights) { + LogView.this.textField.setStyleRange(style); + } + + for (StyleRange style : nonHighlightStyles) { + LogView.this.textField.setStyleRange(style); + } + } catch (Throwable e) { + e.printStackTrace(); + } + } + }); + } + } + private static Color getSystemColor(final int swtColor) { final Color[] color = new Color[1]; @@ -102,6 +196,9 @@ currentLevel = LogService.LOG_INFO; } */ + if (HIGHLIGHT_TEXT) { + Activator.dataManager.addDataManagerListener(this); + } } /** @@ -212,18 +309,36 @@ try { String message = entry.getMessage(); if (goodMessage(message)) { - // Not all messages end w/ a new line, but they need to to print properly. + /* Not all messages length w/ a new line, + * but they need to to print properly. + */ if (!message.endsWith("\n")) { message += "\n"; } + + if (HIGHLIGHT_TEXT) { + ServiceReference serviceReference = entry.getServiceReference(); + + if (serviceReference != null) { + int currentTextLength = LogView.this.textField.getText().length(); + Bounds highlightBounds = + new Bounds(currentTextLength, message.length()); + LogView.this.boundsByServiceReference.put( + serviceReference, highlightBounds); + } + } - SWTUtilities.appendStringWithURL( + Collection<StyleRange> styles = SWTUtilities.appendStringWithURL( LogView.this.textField, LogView.this.urlListener, LogView.this.urlCursorListener, message, COLOR_MAPPING.get("" + entry.getLevel()), - URL_COLOR); + URL_COLOR); + + if (HIGHLIGHT_TEXT) { + LogView.this.nonHighlightStyleRanges.addAll(styles); + } } } catch (Throwable e) { e.printStackTrace(); @@ -242,4 +357,14 @@ return true; } } + + private static class Bounds { + public int start; + public int length; + + public Bounds(int start, int end) { + this.start = start; + this.length = end; + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |