Reputation: 13862
Project Name: File Marker
I personally organize my folders using "Folder Marker". It gives me option to change the folder icon from context menu (right-click menu). I can select planned work, half-done work, done work from contextmenu and my folder icon change according.
Now I want to implement the same concept for individual files.
I want to use Java to implement this solution in windows environment.
Step 1:
To add something in the context menu, the easiest way is to initiate a registry entry.
So what i need to do here is --
Add Context Menu item : File Marker Add Context SubMenu item : planned work, half-done work, done work (submenu of File Marker)
step 2:
Add custom attribute
if(WorkStatus is "planned work") { set an specific icon for the file whose value is "planned work" I think i need to use iconHandler Here }
I need your suggestion on how to implement it.
Is it the right approach for this solution. Or is there a better solution?
I also would be glad to have sample code for step1 and step2
Thanks @Brian
Upvotes: 0
Views: 3174
Reputation: 48211
Well, I have some good news and some bad news...
Since you are talking about "Registry entries", I assume you are under Windows. In that case, I am afraid you cannot change the icon of a specific file, unless it is a shortcut or a .EXE or .DDL file (which can contain resources, such as icons). What you can change, is the default icon for the file type, meaning all files of the same type would be affected (and that would clearly not match your requirement).
In order to achieve the structure you described (a "File Maker" submenu, with multiple entries for the various states ("Planned work", "Half-done work" etc)) in Windows 7 or later, you need to create a static Cascading Menu as described here. (In WindowsXP and earlier you need to implement a ContextMenuHandler which is a significantly more complex task - which I do not intend to cover in this asnwer).
Sample code:
You can run the following commands from a command prompt (with administrative priviledges) or copy and paste the commands in a .BAT file and then run it as administrator. What these commands do is: first register a submenu for each file (see "*" in registry key) and the available submenu-entries and then set the display-text and command for every submenu-entry.
CAUTION: You are adviced to always back-up the registry before making any modifications. The registry is a delicate construct, so please handle will extra care and at your own risk :)
REG ADD "HKEY_CLASSES_ROOT\*\shell\File Marker" ^
/v "MUIVerb" /t REG_SZ /d "File Marker" /f
REG ADD "HKEY_CLASSES_ROOT\*\shell\File Marker" ^
/v "SubCommands" /t REG_EXPAND_SZ /d "WinIconChanger.PLANNED_WORK;WinIconChanger.HALF-DONE_WORK;WinIconChanger.DONE_WORK" /f
REG ADD "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.PLANNED_WORK" ^
/v "MUIVerb" /t REG_SZ /d "Planned work" /f
REG ADD "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.PLANNED_WORK\command" ^
/ve /t REG_SZ /d "\"C:\path\to\jre\bin\javaw.exe\" -jar \"C:\path\to\WinIconChanger.jar\" \"PLANNED_WORK\" \"%%1\"" /f
REG ADD "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.HALF-DONE_WORK" ^
/v "MUIVerb" /t REG_SZ /d "Half-done work" /f
REG ADD "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.HALF-DONE_WORK\command" ^
/ve /t REG_SZ /d "\"C:\path\to\jre\bin\javaw.exe\" -jar \"C:\path\to\WinIconChanger.jar\" \"HALF-DONE_WORK\" \"%%1\"" /f
REG ADD "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.DONE_WORK" ^
/v "MUIVerb" /t REG_SZ /d "Done work" /f
REG ADD "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.DONE_WORK\command" ^
/ve /t REG_SZ /d "\"C:\path\to\jre\bin\javaw.exe\" -jar \"C:\path\to\WinIconChanger.jar\" \"DONE_WORK\" \"%%1\"" /f
PAUSE
Do not forget to replace "C:\path\to\WinIconChanger.jar" and "C:\path\to\jre\bin\javaw.exe" with the actual paths in your system.
In order to undo the modifications done to the registry, you can execute these commands (or save them in a .BAT file and run it) with administrative priviledges:
REG DELETE "HKEY_CLASSES_ROOT\*\shell\File Marker" /f
REG DELETE "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.PLANNED_WORK" /f
REG DELETE "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.HALF-DONE_WORK" /f
REG DELETE "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\WinIconChanger.DONE_WORK" /f
PAUSE
Of course, you will need a .JAR file that will take two arguments, namely the state of work (e.g. PLANNED_WORK, DONE_WORK etc) and the path to the right-clicked file, and does something useful with them (if you could only change the icon of the file :D).
Here is an example class:
class WinIconChanger {
static public void main(String[] args) {
String filePath = "UNKNOWN";
String state = "UNKNOWN";
if (args.length == 2) {
filePath = args[1];
switch (args[0]) {
case "PLANNED_WORK":
case "HALF-DONE_WORK":
case "DONE_WORK":
state = args[0];
break;
default:
break;
}
}
if ("UNKNOWN".equals(state)) {
javax.swing.JOptionPane.showMessageDialog(
null,
"Unknown file or state !",
"WinIconChanger Error",
javax.swing.JOptionPane.ERROR_MESSAGE);
} else {
/*
* Change the icon of the file specified in args[1]
* according to the state specified in args[0]
*/
String msg = String.format(
"Let's assume I just changed the icon of '%s' to %s !",
filePath, state);
javax.swing.JOptionPane.showMessageDialog(
null,
msg,
"WinIconChanger Info",
javax.swing.JOptionPane.INFORMATION_MESSAGE);
}
}
}
(This is just a sketchy example and not production-ready code. You need to verify that the second argument does indeed correspond to a path to an existing file, fine-tune the error-messages, catch Exceptions etc.)
Upvotes: 1