Eclipse plugin display text which is of any language - java

i am new to eclipse plugin development. i created a plugin which loads a unicode saved text file from the folder & displays it on a dialog & also setting it for the Label. If it is, in an English Language, everything is working fine. But if i try to load any other language text, it is displaying empty. How can i get through this.
Here is some code on how i am displaying it on dialog:
Shell shell = Display.getCurrent().getActiveShell();
// Loading the file by creating the assets folder & Temp.txt file inside
// Eclipse Plugin
URL url = FileLocator.find(bundle, new Path("assets/Temp.txt"), null);
URL fileUrl = null;
try {
fileUrl = FileLocator.toFileURL(url);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
File file = new File(fileUrl.getPath());
String Message = "";
try {
if (file.exists()) {
BufferedReader in = new BufferedReader(
new FileReader(file));
String str;
while ((str = in.readLine()) != null) {
if (str.contains(LanguageSelected)) {
System.out.println(str);
Message = Message + "\n" + str;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
// Creating the dialog
Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
...
// Adding Label to dialog
Label LabelMessage = new Label(dialog, SWT.LEFT);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 6;
// Adding Message to the dialog
LabelMessage.setLayoutData(data);
LabelMessage.setBackground(new Color(null, 255, 255, 255)); // White
LabelMessage.setText(Message);

In your launch configuration, make sure you are setting the -nl parameter to the language you want to display. See http://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/reference/misc/runtime-options.html for information.

Related

take screenshot using printscreen button and save it in word doc

Screenshot using PrintScreenand save it in work doc. I tried below but not working. i need a complete code to take screenshot if i press printscreen button and save it in word doc . i need to give doc name dynamically in cmd. until i stop he program, it have to take screen shot and save it in same doc.
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException, AWTException {
try {
// Set the look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// Create a KeyEventDispatcher to listen to keyboard events
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_PRINTSCREEN) {
// Take a screenshot when the print screen button is pressed
try {
// Create a Robot object
Robot robot = new Robot();
// Get the size of the screen
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
// Capture the screen image
BufferedImage screenImage = robot.createScreenCapture(screenRect);
// Get the current date and time
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String fileName = "screenshot_" + dateFormat.format(new Date()) + ".png";
// Save the screenshot to a file
ImageIO.write(screenImage, "png", new File(fileName));
// Create a Word document and add the screenshot
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
// Create a run in the paragraph
XWPFRun run = paragraph.createRun();
// Add the screenshot to the run
FileInputStream stream = new FileInputStream(fileName);
run.addPicture(stream, XWPFDocument.PICTURE_TYPE_PNG, fileName, Units.toEMU(screenImage.getWidth()), Units.toEMU(screenImage.getHeight()));
// Close the stream
stream.close();
// Save the Word document
FileOutputStream out = new FileOutputStream("screenshot_" + dateFormat.format(new Date()) + ".docx");
document.write(out);
out.close();
// Show a message indicating that the screenshot was saved
JOptionPane.showMessageDialog(null, "Screenshot saved in screenshot.docx", "Information", JOptionPane.INFORMATION_MESSAGE);
return false;
}
catch(IOException | AWTException | InvalidFormatException exc){
exc.printStackTrace();}
return false;
}
return false;
}
});
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

How to read file in Java only after JFrame is displayed?

I'm using Java Swing. Initially I want to read a file (which is quite big). So the frame gets displayed after the file is completely. Whereas I want the frame to first load (displayed) and then the file should be read.
class Passwd {
JFrame jfrm;
// other elements
Passwd() {
start();
// Display frame.
jfrm.setVisible(true);
}
public void start() {
// Create a new JFrame container.
jfrm = new JFrame("Password Predictability & Strength Measure");
// Specify FlowLayout for the layout manager.
//jfrm.setLayout(new FlowLayout());
jfrm.setLayout(null);
// Give the frame an initial size.
jfrm.setSize(450, 300);
// align window to center of screen
jfrm.setLocationRelativeTo(null);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// some elements
File file = new File("file.txt");
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// operation
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
// Create the frame on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Passwd();
}
});
}
}
How can I read the file after the frame is displayed?
The JFrame should display immediately, so that's not the problem. The problem is that you're reading in the file on the Swing event thread, and this blocks its ability to display the JFrame. The solution is to not do this, to instead read the file in a background thread, such as via a SwingWorker. This way the JFrame can display unimpeded, and the file reading will not interfere with Swing functioning.
So if the file reading will not change the state of Swing components, use a simple background thread:
new Thread(() -> {
File file = new File("file.txt");
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// operation
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}).start();
If the reading in will change the state of the GUI as the reading occurs, again, use a SwingWorker.
Side issue: avoid using null layouts as they'll come back to bite you.

Saving the value of a Text Area to a text file

How to get the text in a Text Area to be saved to a text file when a button is clicked?
I have implemented the GUI and when the btnContinue button is clicked I want the content of txtOrder to be saved to a text file in notepad.
I'm not a pro programmer in Java so pls correct me if I'm wrong, but I would try this:
FileWriter fw = null;
try
{
fw = new FileWriter("Output.txt",true);
fw.write(txtOrder.getText())
//textArea.write(fw);
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
if (fw!=null)
{
fw.close();
}
}

Browse button not show file path in text box in java

I created a browse button and here is the code for that:
private void lookForExeClick () throws FileNotFoundException{
DirectoryDialog dlg = new DirectoryDialog(Display.getCurrent().getActiveShell());
String directoryPath = dlg.open();
//File file = new File(directoryPath, "MyFileName.txt");
//FileOutputStream outputStream = new FileOutputStream(file);
My text box that I created on the gui is this:
exeLocationText = new Text(controlGroupForSingleRun, SWT.BORDER);
exeLocationText.setText("");
data = new GridData();
data.widthHint = 265;
exeLocationText.setLayoutData(data);
How do I get the filepath that I choose in the directory dialog box after I click browse into the text box that I created in java. Any advice would be helpful. Thanks.
In your click handler, you would just have:
String directoryPath = dlg.open();
if(directoryPath != null)
exeLocationText.setText(directoryPath);

Modify layout margin then generate an image from this layout

I have a weird problem,
I have an activity, within this activity I have a layout I want to make an image of it in order to share it on social networks.
This layout contains differents dynamic images and texts. That's why I can't just store it on as a static image and share it on demand. I need to generate the image right when the user touch the share button.
The problem is that I need to adjust the layout before sharing, what I do ?
ImageView profilPic = (ImageView)dashboardView.findViewById(R.id.profilePic);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)profilPic.getLayoutParams();
params.setMargins(10, 62, 0, 0);
profilPic.invalidate();
profilPic.requestLayout();
First I modify the margin of my layout, then I make an image of it
Bitmap bitmap = null;
try {
bitmap = Bitmap.createBitmap(dashboardView.getWidth(),
dashboardView.getHeight(), Bitmap.Config.ARGB_4444);
dashboardView.draw(new Canvas(bitmap));
} catch (Exception e) {
// Logger.e(e.toString());
}
FileOutputStream fileOutputStream = null;
File path = Environment
.getExternalStorageDirectory();
File file = new File(path, "wayzupDashboard" + ".png");
try {
fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bitmap.compress(CompressFormat.PNG, 100, bos);
try {
bos.flush();
bos.close();
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
And finally I share it.
Basically it works, except that it captures the layout BEFORE redrawing the layout with modified layoutParams.
I need to capture this layout once and only when the new layout parameters are took into account, after layout is redraw.
If I remove the capture code, well it works, when I touch the share button I see the layout moving. But when I have the capture code in place it just capture the layout before modifying margins.
How can I ensure that the layout is redraw before capturing it ?
For the record, to correctly capture the layout only once the view was setup I needed to first setup the view and then capture the layout in a delayed thread. This is the only way I found to make it works.
public void onShareButton() {
dashboardController.setupViewBeforeSharing();
new Timer().schedule(new TimerTask() {
#Override
public void run() {
Bitmap bitmap = null;
try {
bitmap = Bitmap.createBitmap(dashboardView.getWidth(),
dashboardView.getHeight(), Bitmap.Config.ARGB_4444);
dashboardView.draw(new Canvas(bitmap));
} catch (Exception e) {
// Logger.e(e.toString());
}
FileOutputStream fileOutputStream = null;
File path = Environment
.getExternalStorageDirectory();
File file = new File(path, "wayzupDashboard" + ".png");
try {
fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bitmap.compress(CompressFormat.PNG, 100, bos);
try {
bos.flush();
bos.close();
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_TEXT, R.string.addPassengerButton);
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + file.getAbsolutePath()));
startActivity(Intent.createChooser(share, "Share image"));
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
dashboardController.setupViewAfterSharing();
}
});
}
}, 300);
}
An easier way, is to set-up a listener for layout pass on the dashboardView view.
Setting-up listener using View.addOnLayoutChangeListener() - addOnLayoutChangeListener.
Set the listener before changing the layout parameters and remove it after the image was saved to persistence.
Hope it'll add any improvement.

Categories

Resources