Skip to content

Commit

Permalink
#88 added new clear log file button
Browse files Browse the repository at this point in the history
  • Loading branch information
hufoAA committed Jan 23, 2018
1 parent 4868005 commit 41c5cc8
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 33 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public interface ILogViewerConstants {
public static final String IMG_COPY_ACTIVE = "IMG_COPY_ACTIVE"; //$NON-NLS-1$
public static final String IMG_COPY_PASSIVE = "IMG_COPY_PASSIVE"; //$NON-NLS-1$
public static final String IMG_LOG_VIEWER = "IMG_LOG_VIEWER";
public static final String IMG_FILE_CLEAR_ACTIVE = "IMG_CLEAR_ACTIVE"; //$NON-NLS-1$
public static final String IMG_FILE_CLEAR_PASSIVE = "IMG_CLEAR_PASSIVE"; //$NON-NLS-1$

// Preferences -------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.Buffer;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
Expand All @@ -42,6 +44,7 @@
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.TabFolder;
Expand All @@ -50,11 +53,17 @@
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IOConsole;
import org.eclipse.ui.console.IOConsoleOutputStream;
import org.eclipse.ui.console.TextConsole;
import org.eclipse.ui.part.ViewPart;

import de.anbos.eclipse.logviewer.plugin.LogFile.LogFileType;
import de.anbos.eclipse.logviewer.plugin.action.ClearHistoryAction;
import de.anbos.eclipse.logviewer.plugin.action.CloseAllFilesViewAction;
import de.anbos.eclipse.logviewer.plugin.action.FileClearAction;
import de.anbos.eclipse.logviewer.plugin.action.FileCloseViewAction;
import de.anbos.eclipse.logviewer.plugin.action.FileEncondingViewAction;
import de.anbos.eclipse.logviewer.plugin.action.FileOpenViewAction;
Expand All @@ -66,17 +75,13 @@
import de.anbos.eclipse.logviewer.plugin.action.StopTailOnCurrentFileViewAction;
import de.anbos.eclipse.logviewer.plugin.action.TabRenameAction;
import de.anbos.eclipse.logviewer.plugin.action.delegate.FileOpenViewActionDelegate;
import de.anbos.eclipse.logviewer.plugin.file.FileTail;
import de.anbos.eclipse.logviewer.plugin.file.document.LogDocument;
import de.anbos.eclipse.logviewer.plugin.preferences.FileHistoryTracker;
import de.anbos.eclipse.logviewer.plugin.preferences.PreferenceValueConverter;
import de.anbos.eclipse.logviewer.plugin.ui.menu.LocalPullDownMenu;
import de.anbos.eclipse.logviewer.plugin.viewer.LogFileViewer;

import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IOConsole;
import org.eclipse.ui.console.IOConsoleOutputStream;

public class LogViewer extends ViewPart {

// Attribute ---------------------------------------------------------------
Expand Down Expand Up @@ -108,6 +113,7 @@ public class LogViewer extends ViewPart {
private StopTailOnAllFileViewAction stopTailOnAllFiles;
private FileEncondingViewAction fileEncodingAction;
private TabRenameAction tabRenameAction;
private FileClearAction fileClearAction;

private int monitorCounter;
private int monitorCounterMax;
Expand Down Expand Up @@ -172,6 +178,7 @@ public void closeCurrentLogFile() {
} catch(IOException e) {
logger.logError("unable to remove the current; active tab"); //$NON-NLS-1$
}

int index = tabfolder.getSelectionIndex();
getSelectedItem().dispose();
if (!greyAllOutIfNoFiles()) {
Expand All @@ -183,7 +190,38 @@ public void closeCurrentLogFile() {
}
}

public void closeAllLogFiles() {
public void clearCurrentLogFile() {
try {
LogFileType type = getSelectedTab().getDocument().getFile().getType();
if (type == LogFileType.LOGFILE_ECLIPSE_CONSOLE) {

final IConsole con = getSelectedTab().getDocument().getReader().getConsoleTail().getConsole();
if (con instanceof TextConsole) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
((TextConsole) con).clearConsole();
}
});
} else {
logger.logWarning("Console" + "[ " + con.getName() + " ]" + " clear not supported");//$NON-NLS-1$
}
} else {
FileTail file = getSelectedTab().getDocument().getReader().getFileTail();
Buffer buffer = file.getBuffer();
if (buffer != null) {
file.stopFileMapping(buffer);
}
PrintWriter pw = new PrintWriter(getCurrentLogFilePath());
pw.write(" ");
pw.close();
}
} catch (Exception e) {
logger.logError("unable to clear selected file: " + getCurrentLogFilePath()); //$NON-NLS-1$
}
refreshCurrentLogFile();
}

public void closeAllLogFiles() {
Iterator<String> keyIterator = logTab.keySet().iterator();
while(keyIterator.hasNext()) {
Object key = keyIterator.next();
Expand Down Expand Up @@ -264,6 +302,7 @@ boolean greyAllOutIfNoFiles() {
startTailOnAllFiles.setEnabled(false);
stopTailOnAllFiles.setEnabled(false);
tabRenameAction.setEnabled(false);
fileClearAction.setEnabled(false);
resetMonitorCounter();
return true;
}
Expand Down Expand Up @@ -352,15 +391,16 @@ public void openLogFile(LogFile file) {
if(!logTab.containsKey(key)) {
try {
if (file.getNamePattern().equals(LogViewerPlugin.getResourceString("logviewer.plugin.console.name"))) {
createConsole();
createConsole();
}

String encoding = LogViewerPlugin.getDefault().getPreferenceStore().getString(ILogViewerConstants.PREF_ENCODING);
LogDocument document = new LogDocument(file, encoding);
TabItem item = new TabItem(tabfolder, 0);
LogDocument document = new LogDocument(file,encoding);
TabItem item = new TabItem(tabfolder,0);
item.setControl(viewer.getControl());
item.setText(file.getNamePattern());
item.setToolTipText(file.getPath());
logTab.put(key,new LogFileTab(key, item, document));
logTab.put(key,new LogFileTab(key,item,document));
document.addDocumentListener(documentListener);

// restore monitor status
Expand All @@ -376,6 +416,7 @@ public void openLogFile(LogFile file) {
tabRenameAction.setEnabled(true);
startTailOnAllFiles.setEnabled(true);
stopTailOnAllFiles.setEnabled(true);
fileClearAction.setEnabled(true);
} catch(Exception e) {
logger.logError("unable to open the selected logfile",e); //$NON-NLS-1$
LogViewerPlugin.getDefault().showErrorMessage(LogViewerPlugin.getResourceString("main.error.open.file",new String[]{file.getPath()})); //$NON-NLS-1$
Expand Down Expand Up @@ -446,6 +487,10 @@ public void setCurrentLogFileTabName(String name) {
getSelectedTab().getDocument().getFile().setNamePattern(name);
}

public String getCurrentLogFilePath() {
return getSelectedTab().getDocument().getFile().getPath();
}

public void dispose() {
viewer.removeListeners();
storeAllCurrentlyOpenFiles();
Expand Down Expand Up @@ -504,6 +549,7 @@ private void fillLocalPullDown(IMenuManager manager) {
menu.addAction(fileOpenAction);
menu.addFilelist();
menu.addAction(clearHistoryAction);
menu.addAction(fileClearAction);
menu.addSeparator();
menu.addAction(refreshCurrentFileAction);
menu.addAction(startTailOnCurrentFile);
Expand All @@ -527,6 +573,7 @@ private void fillContextMenu(IMenuManager manager) {
manager.add(startTailOnCurrentFile);
manager.add(stopTailOnCurrentFile);
manager.add(fileCloseAction);
manager.add(fileClearAction);
manager.add(new Separator());
manager.add(fileEncodingAction);
manager.add(tabRenameAction);
Expand All @@ -536,6 +583,7 @@ private void fillContextMenu(IMenuManager manager) {
private void fillLocalToolBar(IToolBarManager manager) {
manager.add(fileOpenAction);
manager.add(preferencesAction);
manager.add(fileClearAction);
manager.add(new Separator());
manager.add(refreshCurrentFileAction);
manager.add(startTailOnCurrentFile);
Expand Down Expand Up @@ -624,6 +672,9 @@ private void makeActions() {
// tab rename action
tabRenameAction = new TabRenameAction(this,parent.getShell());
tabRenameAction.setEnabled(false);
// clear file
fileClearAction = new FileClearAction(this,parent.getShell());
fileClearAction.setEnabled(true);
}

private void storeAllCurrentlyOpenFiles() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ menu.findreplace.text=Find
menu.findreplace.tooltip=Find any string
menu.tabrename.text=Rename tab...
menu.tabrename.tooltip=Rename active tab
menu.file.clear.text=Clear
menu.file.clear.tooltip=Clear Logfile

main.error.open.file=Unable to open resource {0}

Expand Down Expand Up @@ -139,6 +141,9 @@ dialog.encoding.title=choose encoding
dialog.encoding.defaults.button=Restore Default
dialog.tabrename.title=rename tab

dialog.file.clear.title=Confirm Logfile Clear
dialog.file.clear.text=Are you sure you want to clear the log file?\nYou won't be able to revert!

# misc
misc.clearhisrory.title=Clear history
misc.clearhisrory.text=Do You want to clear the log history?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ private static ImageRegistry initializeImageRegistry() {
declareRegistryImage(ILogViewerConstants.IMG_RENAME_ACTIVE, "active/rename_active.gif"); //$NON-NLS-1$
declareRegistryImage(ILogViewerConstants.IMG_RENAME_PASSIVE, "passive/rename_passive.gif"); //$NON-NLS-1$
declareRegistryImage(ILogViewerConstants.IMG_COPY_ACTIVE, "active/copy_active.gif"); //$NON-NLS-1$
declareRegistryImage(ILogViewerConstants.IMG_COPY_PASSIVE, "active/copy_passive.gif"); //$NON-NLS-1$
declareRegistryImage(ILogViewerConstants.IMG_COPY_PASSIVE, "passive/copy_passive.gif"); //$NON-NLS-1$
declareRegistryImage(ILogViewerConstants.IMG_FILE_CLEAR_ACTIVE, "active/clear_active.gif"); //$NON-NLS-1$
declareRegistryImage(ILogViewerConstants.IMG_FILE_CLEAR_PASSIVE, "passive/clear_passive.gif"); //$NON-NLS-1$
declareRegistryImage(ILogViewerConstants.IMG_LOG_VIEWER, "logviewer.gif");

return imageRegistry;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright (c) 2009 - 2018 by Andre Bossert
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Andre Bossert - initial API and implementation and/or initial documentation
*******************************************************************************/

package de.anbos.eclipse.logviewer.plugin.action;

import org.eclipse.swt.widgets.Shell;

import de.anbos.eclipse.logviewer.plugin.ILogViewerConstants;
import de.anbos.eclipse.logviewer.plugin.LogViewer;
import de.anbos.eclipse.logviewer.plugin.LogViewerPlugin;
import de.anbos.eclipse.logviewer.plugin.UIImages;
import de.anbos.eclipse.logviewer.plugin.action.delegate.FileClearActionDelegate;

public class FileClearAction extends AbstractViewAction {

// Constructor
// ------------------------------------------------------------------

public FileClearAction(LogViewer view, Shell shell) {
super(view, shell, new FileClearActionDelegate());
}

// Public
// -----------------------------------------------------------------------

public void init() {
this.setText(LogViewerPlugin.getResourceString("menu.file.clear.text")); //$NON-NLS-1$
this.setToolTipText(LogViewerPlugin.getResourceString("menu.file.clear.tooltip")); //$NON-NLS-1$
this.setImageDescriptor(UIImages.getImageDescriptor(ILogViewerConstants.IMG_FILE_CLEAR_ACTIVE));
this.setDisabledImageDescriptor(UIImages.getImageDescriptor(ILogViewerConstants.IMG_FILE_CLEAR_PASSIVE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2009 - 2018 by Andre Bossert
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Andre Bossert - initial API and implementation and/or initial documentation
*******************************************************************************/

package de.anbos.eclipse.logviewer.plugin.action.delegate;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import de.anbos.eclipse.logviewer.plugin.LogFile.LogFileType;
import de.anbos.eclipse.logviewer.plugin.LogViewer;
import de.anbos.eclipse.logviewer.plugin.LogViewerPlugin;

public class FileClearActionDelegate implements ILogViewerActionDelegate {

/*
* (non-Javadoc)
*
* @see
* de.anbos.eclipse.logviewer.plugin.action.ILogfileAction#run(de.anbos.
* eclipse.logviewer.plugin.LogViewer, org.eclipse.swt.widgets.Shell)
*/
public void run(LogViewer view, Shell shell) {

LogFileType type = view.getCurrentDocument().getFile().getType();
if (type == LogFileType.LOGFILE_ECLIPSE_CONSOLE) {
view.clearCurrentLogFile();
} else if (confirmFileClear(shell)) {
view.clearCurrentLogFile();
}
}

private boolean confirmFileClear(Shell shell) {
return MessageDialog.openQuestion(shell, LogViewerPlugin.getResourceString("dialog.file.clear.title"), //$NON-NLS-1$
LogViewerPlugin.getResourceString("dialog.file.clear.text")); //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/*******************************************************************************
* Copyright (c) 2009 - 2018 by Andre Bossert
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
*
* Contributors:
* Andre Bossert - initial API and implementation and/or initial documentation
*******************************************************************************/


package de.anbos.eclipse.logviewer.plugin.file;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
Expand All @@ -32,12 +33,20 @@ public BackgroundReader(LogFileType type, String path, String namePattern, Chars
consoleTail = new ConsoleTail(path, namePattern, listener);
}
}

public void setMonitorStatus(boolean monitor) {
if (type == LogFileType.LOGFILE_SYSTEM_FILE) {
fileTail.setMonitorStatus(monitor);
} else if (type == LogFileType.LOGFILE_ECLIPSE_CONSOLE) {
consoleTail.setMonitorStatus(monitor);
}
}

public FileTail getFileTail() {
return fileTail;
}

public ConsoleTail getConsoleTail() {
return consoleTail;
}
}
Loading

0 comments on commit 41c5cc8

Please sign in to comment.