Disabling a menu item in Eclipse during startup - java

I am developing an Eclipse plugin. I need to check if the nvcc is present at start-up, and if not, then disable a particular menu item. Here is my code till now:
package Startup;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.eclipse.ui.IStartup;
public class StartCheck implements IStartup {
public void earlyStartup() {
// This method checks for presence of nvcc when Eclipse starts-up.
String command="nvcc -version";
Runtime run = Runtime.getRuntime();
Process pr;
try {
pr = run.exec(command);
pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
//print-out the nvcc version
System.out.println(buf.readLine());
}catch (IOException e) {
//disable all buttons since no further task can be done, prompt user to install nvcc.
System.out.println("nvcc not present, freezing the energy estimation plugin...");
disableMenu();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void disableMenu(){
}
}
As you can see, the disable menu method does not contain anything. Can someone please help me with this? I have to basically disable a particular menu item if an IOException occurs in the above code.
Thanks!

Related

Java runtime.exec process hangs

So basically my program creates a process of ffplay(program based on ffmpeg that plays audio and video), but it hangs(have to force kill the program, loading wheel of death, etc.) about 15 seconds into playing. What is also interesting is that I call it with -autoexit which should kill the program at the end of the audio or video, but it doesn't close.
import java.lang.Runtime;
import java.lang.Process;
import java.io.IOException;
public class FFPlay
{
Process proc;
public FFPlay(String fileToPlay, String fileName)
{
try
{
String[] command = {"/Users/myusername/Documents/Java Projects/SwingTest/ffplay", "-window_title", fileName, "-x", "500", "-y", "500", "-autoexit", fileToPlay};
proc = Runtime.getRuntime().exec(command);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
So what am I doing wrong? Is this a bad method for executing binaries? I don't know much about the Runtime and Process classes to begin with.
Edit: Method that calls the above class
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
int index = songList.locationToIndex(e.getPoint());
FFPlay player = new FFPlay(songList.getModel().getElementAt(index).toString(),songList.getModel().getElementAt(index).getName());
}
}

Message console on JFrame does not work by double click on jar

So I created a message console. And used append to display messages and it works perfectly by running java -jar JavaProgram, however when I double click on it the application runs and I see the JFrame but nothing is displayed. The text that I did append is not present.
By the way, double clicking it on windows does display the message output but on my linux system nothing is displayed.
I'm running the same version of java on each machine.
Code Below:
package pdfCounter;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class App {
public static JTextArea textComponent;
public static int count;
public static void main(String args[]) {
try {
JFrame somePanel = new JFrame();
somePanel.add(new JLabel(" Message Console"), BorderLayout.NORTH);
textComponent = new JTextArea(5, 10);
somePanel.setVisible(true);
somePanel.setSize(900, 300);
somePanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
somePanel.add(new JScrollPane(textComponent));
pdfCounter.MessageConsole mc = new pdfCounter.MessageConsole(textComponent);
mc.redirectOut(null, System.out);
mc.redirectErr(Color.RED, null);
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
File root = new File(s);
count = 0;
boolean recursive = true;
Collection files = FileUtils.listFiles(root, null, recursive);
for (Iterator iterator = files.iterator(); iterator.hasNext(); ) {
try {
File file = (File) iterator.next();
if (file.getName().endsWith(".pdf")) {
String absoluteFile = file.getAbsolutePath();
append(absoluteFile);
PDDocument doc = PDDocument.load(new File(file.getAbsolutePath()));
count = doc.getNumberOfPages() + count;
doc.close();
}
} catch (Exception e) {
continue;
}
}
}
catch(Exception e) {
e.printStackTrace();
}
try (PrintStream out = new PrintStream(new FileOutputStream("NumberOfPages.txt"))) {
out.print(count);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void append(String absolutePath) {
textComponent.append(absolutePath + "\n");
}
}
when it gets to the `append(absoluteFile); part thats where the problem lies as it only appends on windows not linux.
UPDATE: I figured that opening it from a different file manager with double click, makes it work. With Nautilus is does not open, even when i choose to run it with java 8 or 9. Opening it with thunar(Different file manager) makes it work no problem with double clicking it. Both are set to run with java 9. I think it has something to do with folder permissions because if i run nautilus as root user, it works when double clicking.

How to Change a .PNG file name through Java/Eclipse

I was wondering if anyone can help me solve this. I want to change/rename the name of a file .png as soon as I create it using java Eclipse through Linux. So when the file is saved into a folder I want the user to be able to change the name of it to whatever the user wants. How do I do that?
Here is my code...
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class adbPicture implements ActionListener {
#Override
public void actionPerformed(ActionEvent arg0) {
// exec.(points at whatever .sh file you want to run in the folder)
try {
Process proc = Runtime.getRuntime().exec("sh /home/local/ANT/arthm/Desktop/stuff/pics1.sh);
BufferedReader read = new BufferedReader(new InputStreamReader( proc.getInputStream()));
try {
proc.waitFor();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
while (read.ready()) {
System.out.println(read.readLine());
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error 5! You Messed Up!");
}
int picreply = JOptionPane.showConfirmDialog(null, "Would you like to change the name?", null, JOptionPane.YES_NO_OPTION);
if(picreply == JOptionPane.YES_OPTION){
String namestuff = JOptionPane.showInputDialog(null, "Please Input Name");
// Here in this area is where I want to add the code to change the name of the file...
}
else {
System.exit(0); // also this makes me exit the whole program, how do I only exit the //showConfirmDialog box?
}
}
}
You can use File#renameTo
File original = new File("...");
original.renameTo(new File("..."));
Note. If you change the location that the file is stored in, this will act like a move
String fileRenamed = JOptionPane.showInputDialog(null, "Please Input New Name");
Process proc = Runtime.getRuntime().exec("mv "+ pathToFile+namestuff+" "+pathToFile+fileRenamed);

Java clipboard as stack in paste override

I have done my research, no useful results found.
Here is the deal, I'm writing a 'new' clipboard that works like a stack instead of a 'area'. And I'm brave or stupid enoght to do that in Java. So far in my tests to see if this is possible I have managed to create this stack behavior. The only problem I'm getting is that sometimes, mainly when I paste the top of the stack (pop operation), it doesn't pop or for some other reason it pastes twice.
Example:
If i copy this three words: Carlos, Lucas, Eastwood
The stack clipboard behaves like this at paste: Eastwood, Eastwood, Lucas, Carlos
I'm using JNativeHooks for reading the system keypresses and determining when the user is pasting.
I think what is happening is that the system is pasting before my code... Well, here is the code anyway (It is a test, that explains why it is badly commented):
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.util.Stack;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.NativeInputEvent;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class Test3 implements NativeKeyListener {
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
Stack<Transferable> clipStack = new Stack<>();
public static void main(String[] args) {
try {
GlobalScreen.registerNativeHook();
} catch (NativeHookException ex) {
System.err
.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
Test2 t2 = new Test2();
// Construct the example object and initialze native hook.
GlobalScreen.getInstance().addNativeKeyListener(t2);
}
#Override
public void nativeKeyPressed(NativeKeyEvent ev) {
// Copy
if (ev.getKeyCode() == NativeKeyEvent.VK_C
&& NativeInputEvent.getModifiersText(ev.getModifiers()).equals(
"Ctrl")) {
// Clip the pop
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
clipStack.push(sysClip.getContents(null));
System.out.println("Ctrl+C : Stack(" + clipStack.size() + ")");
}
// Paste
if (ev.getKeyCode() == NativeKeyEvent.VK_V
&& NativeInputEvent.getModifiersText(ev.getModifiers()).equals(
"Ctrl")) {
// Clip the pop
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (clipStack.size() > 1) {
sysClip.setContents(clipStack.pop(), null);
} else
sysClip.setContents(clipStack.peek(), null);
System.out.println("Ctrl+V : Stack(" + clipStack.size() + ")");
}
}
#Override
public void nativeKeyReleased(NativeKeyEvent e) {
}
#Override
public void nativeKeyTyped(NativeKeyEvent e) {
}
}
I would think the same as you suggested.
Have a copy and paste key combination something different from ctrl+c and ctrl+v and you can even bypass using the system clipboard. Simply push the selected text and pop the text directly to your text pointer in your application provided of course that you are using this stack behavior for only a particular application.
The system retains it's own copy of latest clip and it gets a duplicate from your sysClip.setContents(clipStack.pop(), null) on paste operation.
I don't have much idea on disabling the system behavior. You can do research on that. But you can always make sure that it's the only problem by changing the key combination.

JOptionPane: Installing Custom icon has no effect when look and feel set

I would like to install a custom icon in place of the standard JOptionPane information icon..
I tried
ImageIcon myCustomIcon = ...;
UIManager.put("OptionPane.informationIcon", myCustomIcon);
However, apparently this has no effect due to the following line:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Commenting out this line gives the correct behaviour. I have of course tried putting the icon into the UIManager both before/after the look and feel is being set.
Can I somehow combine the two to override the information icon globally?
I am working on Ubuntu 10.04 right now..
Regards,
Morten
Works fine for me using the Metal and Windows LAF's.
Maybe your LAF doesn't support the UIManager property. Check out UIManager Defaults for a list of properties.
If you need more help the post your SSCCE that demonstrates the problem.
Have you tried to specify your icon after setting look&feel, in such a way:
JOptionPane.showMessageDialog(frame,
"Eggs are not supposed to be green.",
"Inane custom dialog",
JOptionPane.INFORMATION_MESSAGE,
myCustomIcon);
Update:
The following code works fine on my Windows 7:
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class OptionPaneIcon {
public static void main (String[] args) {
ImageIcon myCustomIcon = loadImageIcon("image.png");
UIManager.put("OptionPane.informationIcon", myCustomIcon);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JOptionPane.showMessageDialog(null, "Hello!");
}
/** Returns an ImageIcon, or null if the path was invalid. */
private static ImageIcon loadImageIcon(String path) {
URL imgURL = OptionPaneIcon.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
P.S. Sorry for my impatience.

Categories